Question

In: Computer Science

Write a python program that simulates a simple dice gambling game. The game is played as...

Write a python program that simulates a simple dice gambling game.

The game is played as follows:

  • Roll a six sided die.
  • If you roll a 1, 2 or a 3, the game is over.
  • If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again.
  • With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which time the game is over and you keep whatever winnings you have accumulated.

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.**

Solutions

Expert Solution

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}")


Related Solutions

PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls 2-6 then they can either roll again or hold. If the player...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] The following table shows the 36 possible combinations of the two dice. Your program should...
1. Write a program that plays a simple dice game between the computer and the user....
1. Write a program that plays a simple dice game between the computer and the user. When the program runs, it asks the user to input an even number in the interval [2..12], the number of plays. Display a prompt and read the input into a variable called ‘plays’ using input validation. Your code must ensure that the input always ends up being valid, i.e. even and in [2..12]. 2. A loop then repeats for ‘plays’ iterations. Do the following...
In Python write a function that simulates the throwing of any even numbered dice which will...
In Python write a function that simulates the throwing of any even numbered dice which will be passed on as a parameter: customDice(sides). The function will return a random number rolled for that particular type of dice. Write the program that will utilize the customDice function with the following specification: Ask the user to pick which even-sided dice to use. The program will then roll the dice twice. If the total of both rolls is greater than the number of...
If a gambling game is played with expected value $0.40, then there is a 40% chance...
If a gambling game is played with expected value $0.40, then there is a 40% chance of winning. false true
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for this game are special oriental dice. Each die has six different patterns. The six possible patterns are: Fish, Shrimp, Crab, Chicken, goldenCoin, Barrel. This game has three dice. In this game, the player can place his or her bet on any die-pattern. The amount of the winning prize is directly proportional to the number of matched dice after the two dice have been tossed...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for. To have your program roll two dice, use the rand() function this way: die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; Your program then computes and prints the number of rolls it takes to get the...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
Write a hangman program in c++.The game is played as follows: 1. The program selects a...
Write a hangman program in c++.The game is played as follows: 1. The program selects a random word for the user to guess (Read words for the user to guess into an array of strings from a file. The words are: revolutionary, meaning, recovery, compartment, trainer, pursuit, harm, platform, error, confusion) 2. User guesses one letter at a time until either they guess the word, or they run out of guesses. 3. Select a random word from the array of...
Roll dice java program The program simulates a dice roll by generating a random integer between...
Roll dice java program The program simulates a dice roll by generating a random integer between 1 and 6
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT