In: Computer Science
Write a python program that simulates a simple dice gambling game.
The game is played as follows:
Use the randint() function from Python's Random module to get a
die roll result
(see functions for integers).
As an example, run 10,000 simulations of the game (Monte Carlo
method).
Obtain the average amount won and the largest amount won.
**Make sure your simulation is set up as a function using def () with the number of simulations as input and return the average amount won and max amount won from the simulations.**
The code to do so will be somewhat like this:
if the answer helped you please upvote and if you have any doubts please comment i will surely help. please take care of the indentation while copying the code. Check from the screenshots provided.
Code:
import random
def dice_roll():
# initialise the amount won to 0
won = 0
dice_val = random.randint(1,6)
# keep playing if you get a 4,5,6
while dice_val in [4,5,6]:
# add the value won to the won amount
won = won + dice_val
dice_val = random.randint(1,6)
# return the amount won
return won
def simulations(count):
# store the output from dice_roll in a list
outputs = []
for i in range(count):
# for each simulation store the output in a list
outputs.append(dice_roll())
# Find the max and average
maxVal = max(outputs)
averageVal = sum(outputs)/len(outputs)
# return both the values
return maxVal, averageVal
# Test the output using a test function
maxVal, averageVal = simulations(10000)
print(f"The max value in the simulations was: {maxVal}")
print(f"The average value of the simulations was:
{averageVal}")