Write a program that performs a simulation to estimate the p

Write a program that performs a simulation to estimate the probability of rolling five-of-a-kind in single roll of five six-sided dice. Use top-down design, and bottom up implementation. Use functions; define functions that only do one thing (and can be described without the word \"and\"). Document each function with a one-sentence description. Allow the user to specify how many rolls are simulated. Optimize your code by removing all print statements and extraneous calculations during the simulation. Validate the input to be sure the program terminates within ten seconds. (E.g. measure how many simulations can be done in ten seconds, and restrict input to that amount).

Solution

from random import randint #import randint to get a random number between 1 to six for the dice
import time

#taking input from the user for number of simulaations
def number_of_simulations():
    num = int(input(\"Please enter the number of simulations you\'d like: \"))
    #After checking it turns out that approximately 4 million simulations happen in 10 seconds on my computer. Hence accepting input less than that.
    while(num>4000000):
        print(\"Please enter a value less than 4,000,000 so that the simulation completes within 10 seconds.\")
        num = input(\"Please enter the number of simulations you\'d like: \")
    return num

#simulate a single set of 5 dice rolls.
def roll():
    #simulating a dice roll using randint between 1 to 6
    dice_num = randint(1, 6)
    for i in range(0, 4):
        previous_dice_num = dice_num
        dice_num = randint(1, 6)
        if dice_num!=previous_dice_num:
            return False #returning false if any of the numbers is not equal
    return True

#function to simulate the entire experiment
def simulate(n):

    #counter to keep track of favorable outcome
    fav = 0

    #iterating through the number of simulations requested by the user
    for i in range(n):
        if roll():
            fav += 1
    #return the Probability
    return fav*1.0/n

n = number_of_simulations()
start_time = time.time()
print(\"Probability: \", simulate(n))
end_time = time.time()
print(\"Time: \", end_time-start_time)

 Write a program that performs a simulation to estimate the probability of rolling five-of-a-kind in single roll of five six-sided dice. Use top-down design, an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site