Write an Object Oriented Program that performs a simulation to estimate the prob
ID: 3773958 • Letter: W
Question
Write an Object Oriented Program that performs a simulation to estimate the probability of rolling five-of-a-kind in single roll of five six-sided dice. Create a file that defines class Die, which has one instance variable (top) and a roll method for randomly assigning a new value (1 through 6) to top. Create a second file that imports the Die class and c reates five dice. Allow the user to specify how many rolls are to be simulated. Run the simulation and print the probability (Number of Five-of-a-kind rolls divided by total rolls).Explanation / Answer
from random import randrange
def main():
msg()
chances = getInput()
foak = rollNtimes(chances)
printSummary(foak,chances)
def msg():
print('''A Dice Simulation to check probability rolling five-of-a-kind in single roll.''')
#ask how many times to simulate rolling dices
def getInput():
number = eval(raw_input(' Please enter no.of times you want to roll the dices: '))
return number
def rollNtimes(chances):
foak = 0
#roll to the no.of times limit
for i in range(chances):
result = rollOneTime()
foak = diceCheck(result,foak)
return foak
#running each times roll all the dices
def rollOneTime():
result = 'no'
i = 0
#first dice stop rolling.
x = diceRandom()
while diceRandom() == x:
i = i+1
#if all 5 dices has the same value store result
if i == 4:
result = 'yes'
return result
#checking if the result get five-of-a-kind
def diceCheck(result,foak):
if result == 'yes':
foak = foak+1
return foak
#random dice whice has value between 1-6
def diceRandom():
x = randrange(1,7)
return x
def printSummary(foak,times):
print(' For {} times we get FOAK {} times which is {:0.2%}'.format(times,foak,foak/times))
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.