Plz write a code for the answer. Any code ..Python, matlab or c++ ...etc any lan
ID: 3724480 • Letter: P
Question
Plz write a code for the answer. Any code ..Python, matlab or c++ ...etc any language
4.32 In Problems 3.28 and 3.29, we calculated the probabilities of a Yahtzee throw of five dic Ce, all In the game, the player is allowed to keep any number of the five dice (from zero to five), and throw the remaining dice again. The player is then allowed to keep any of th five dice and throw the remaining dice yet again. (The player is even allowed to rethr dice he or she kept on the previous throw.) The best score in the game is called a Yahtz which happens when the player obtains the same number on all five dice. ee For example, the player might throw 2, 2, 4, 5, 6. The playe r might choose to keep the 2, 2 and throw the other three dice again. In the second throw, the player obtains 1, 2, 3, The player keeps the 2 (making three 23) and rethrows the remaining two dice. If those dice come up 2, 2, the player has made Yahtzee. Write a program to simulate Yahtzee. Assume the player is trying to achieve Yahtzee. What is the estimated probability of achieving Yahtzee? (Hint: the exact value is approximately 5%)Explanation / Answer
Python3 Code:
from random import randint
values = [0, 0, 0, 0, 0, 0] #to track count
diceKeep = -1 #yathzee number
diceCount = 0 #count of yathzee number
dice = 5 #throwing dice
throws = 0 #total throws
#roll till dice becomes 0
while dice > 0:
throws = throws + 1 #increment throws
#throw all dice
for i in range(dice):
ind = randint(1, 6)
values[ind-1] = values[ind-1] + 1
count = max(values) #get max value
#if we kept some dice already
if diceKeep != -1:
ind = values.index(count) #get dice number
#if same dice increment count
if diceKeep == ind:
diceCount = diceCount + count
#if different dice
else:
#count is more than previous one update
if count > diceCount:
diceCount = count
diceKeep = ind
#add that value to dice we kept
elif diceKeep != -1:
diceCount = diceCount + values[diceKeep]
#if we haven't keep any dice
elif count>1:
diceCount = count
diceKeep = values.index(count)
#make all counts 0
for i in range(len(values)):
values[i] = 0
dice = 5 - diceCount #update dice throwing
#print yathzee number
print("Yahtzee number:",diceKeep+1)
print("Total throws:",throws)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.