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...
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...
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...
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
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.
Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this program, your code can have user defined functions (but not required), the program must not call on any external functions or modules to handle any of the input, computational, and output requirements. Requirements: There is a customer with the following credential and can only access the system if the Access Code is correct: • Name: Peter Parker, Access Code: 2222 • When the program...
For this problem, you will write a program in which the computer plays a dice game...
For this problem, you will write a program in which the computer plays a dice game called Craps. You might need to explore some Python documentation on random() for this homework assignment. PROVIDE A GRAPHICAL FLOWCHART as part of your submission for this solution Rules: The player rolls two six-sided dice. After rolling, the sum of what the dice show is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask the user if they want to roll again. If they don't want to roll again, the program should end. If they do want to roll again, the new number should display. At a minimum, the die should have 6 sides, but if you want to use a d12 or d20, that is fine too. USE PYTHON
Suppose that a game of chance is played with a pair of fair 10-sided dice (with...
Suppose that a game of chance is played with a pair of fair 10-sided dice (with the sides numbered 1 to 10). In the game, you can pick any number from 1 to 10 and the two dice are then “rolled” in a cage. If $1 is bet and exactly one of the number that you picked is rolled you win $1, and if both of the dice are the number that you picked you win $20 (in each of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT