MATH 335: Simulate the "Full House" problem

Here is R code to run a simulation. Run the simulation and see if you get confirmatory evidence for the .00144 value given in the textbook on page 121.


reps <- 100000  
success <- 0  # success counts how many full houses have been found
deck <- rep(1:13,4)  # for purposes of this problem, suits don't
                     # matter
for (i in 1:reps) {   # begin a loop with index i
         hand <- sample(deck,5)
         success <- success +
                   ( sort(tabulate(hand),decreasing=T)[1]==3 &
           sort(tabulate(hand),decreasing=T)[2]==2 )
}

R wonks tell us that the replicate function is usually more efficient than using 'for' loops. Consider the following re-writing of the previous code? It looks very similar to the previous code, except that I have suppressed the definition of the "hand" variable. Will it produce equivalent results? Explain.


reps <- 100000  
success <- 0  # success counts how many full houses have been found
deck <- rep(1:13,4)  # for purposes of this problem, suits don't
                     # matter
for (i in 1:reps) {   # begin a loop with index i
         success <- success +
             ( sort(tabulate(sample(deck,5)),decreasing=T)[1]==3 &
                sort(tabulate(sample(deck,5)),decreasing=T)[2]==2 )
}

To use the replicate function, we will first define a little function that defines a string of 5 numbers as a full house or not. NOTE: The function below is not very smart; it assumes that the vector you give it for the hand argument will be of length 5. Cut and paste (or type each line in one at a time) to define the function is an R session.


full.house <- function(hand) {
     success <- ( sort(tabulate(hand),decreasing=T)[1]==3 &
            sort(tabulate(hand),decreasing=T)[2]==2 )
     return(success)
 }


Now use replicate to run a simulation that computes empirically the probability of a full house.

  • Using replicate for full house simulation