###################################################################### # # Yahtzee in R # # Rules of Yahtzee (Simplified) # # * Roll five dice, and re-roll some of them until you get a Yahtzee (meaning that all five dice show the same number). # * Roll five dice. Suppose we get 1,2,1,3,1. # * Keep the values that occur the most. In our example, we have three 1s. This is our "current Yahtzee." # * Re-roll the other dice. In this case, we re-roll the two dice that were not 1s. Suppose we get 1 and 4. # * Add to the current Yahtzee. In our example, we have 1,1,1,1,4. Return to second step and repeat until all the values are the same, in which case we have a Yahtzee. # # Question: On average, how many rolls are required to get a Yahtzee? # # Let's explore this question using R. # ###################################################################### ##### Rolling Dice ##### # The sample function can simulate rolling a die: sample(1:6, 1) # The sample function can also simulate rolling many dice: sample(1:6, 5, replace=T) # We can write our own function to make this a bit simpler to use: myDice <- function(n){ sample(1:6, n, replace=T) } ##### Simulate rolling for a Yahtzee ##### totRolls <- 0 # we haven't yet rolled currMatches <- c() # no matches to start totMatches <- length(currMatches) # total matches # roll the correct number of dice currMatches <- c(currMatches, myDice(5 - totMatches)) currMatches # How do we select the most common match? counts <- c() for(i in 1:6){ # this counts how often each value i occurs counts[i] <- sum(currMatches == i) } maxroll <- which.max(counts) # this is a value with the most matches totMatches <- max(counts) # how many matches currMatches <- rep(maxroll, totMatches) # keep only the current matches # Here is another way to do it: counts <- table(currMatches) totMatches <- max(counts) whichNums <- as.integer(names(which(counts == totMatches))) num <- whichNums[1] # choose the smallest number, if there are multiple numbers with totalMatches matches currMatches <- rep(num, totMatches) # now repeat this while totMatches is less than 5... while(totMatches < 5){ #roll the dice currMatches <- c(currMatches, myDice(5 - totMatches)) print(currMatches) #count the number of matches counts <- c() for(i in 1:6){ # this counts how often each value i occurs counts[i] <- sum(currMatches == i) } maxroll <- which.max(counts) # this is a value with the most matches totMatches <- max(counts) # how many matches currMatches <- rep(maxroll, totMatches) # keep only the current matches #increment the counter totRolls <- totRolls + 1 } # how many rolls did it take to get a Yahtzee? print(totRolls) ##### Now write a function to simulate Yahtzee ##### rollYahtzee <- function(){ totRolls <- 0 # we haven't yet rolled currMatches <- c() # no matches to start totMatches <- length(currMatches) # total matches while(totMatches < 5){ # repeat while totMatches is less than 5 # keep track of the number of rolls totRolls <- totRolls + 1 # roll the correct number of dice currMatches <- c(currMatches, myDice(5 - totMatches)) #check for matches matches <- rep(0,6) for(i in 1:6){ # this counts how often each value i occurs matches[i] <- sum(currMatches == i) } max.roll <- which.max(matches) # this is a value with the most matches totMatches <- max(matches) # how many matches currMatches <- rep(max.roll, totMatches) # keep only the current matches } # return the result c(totRolls, currMatches) } # try it out: rollYahtzee() rollYahtzee()[1] # Now simulate Yahtzee many times and record how many rolls it takes. # What is the mean number of rolls? # What proportion of the time do you get Yahtzee in three or fewer rolls? # Make a histogram of the number of rolls until you get Yahtzee