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.
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...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete,...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
This program is in Python ### simulates packets of data arriving at irregular times. The data...
This program is in Python ### simulates packets of data arriving at irregular times. The data gets ### read from the file and then you must place it in a queue where it will wait ### its turn to get processed. The "processing" happens in a threaded subroutine ### called "process_input_data". That function should pull data from the queue, ### hash the student ID number, and then store the data in a hash table which ### you have to implement....
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT