Question
using r studio an example with Yahtzee. Suppose you roll 5 fair six-sided dice. Your goal (in this simplified version) is to roll the highest
using r studio
an example with Yahtzee. Suppose you roll 5 fair six-sided dice. Your goal (in this simplified version) is to roll the highest number with your dice. In standard Yahtzee, you roll the dice the first time, and can choose any of the dice to hold or reroll. After a second roll, you can again choose any of the dice to hold or reroll. After a third roll, you score yourself based on the final results.
Let's see if there is a strategy to maximize our total roll. (This would typically be scored in the "chance" line if playing Yahtzee for real.) The following is a (purposefully) clunky version of code that simulates a process where I choose to reroll a die if it is less than 4.
roll = sample(1:6, 5, replace = TRUE)
for(j in 1:5){
if(roll[j] < 4){
roll[j] = sample(1:6, 1, replace = TRUE)
}
}
for(k in 1:5){
if(roll[k] < 4){
roll[k] = sample(1:6, 1, replace = TRUE)
}
}
total = sum(roll)
Your turn #2
a) What is the purpose of each line of the code?
b) Add a for loop to the code above that repeats the procedure 1000 times and keeps track of the final total. What is the average final total?
c) Modify your code in part b to run the code if you choose to reroll a die if it is less than 6. Then do it for less than 5. Then for less than 3. Then for less than 2. Compare all 5 strategies. Which strategy is the best for maximizing your final total?
d) Modify your code in part b to allow a third rerolling of the dice. How does this change the results?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started