Skip to content

Instantly share code, notes, and snippets.

@is8ac
Created March 18, 2015 00:10
Show Gist options
  • Save is8ac/b191d71ad4a3ee0973cd to your computer and use it in GitHub Desktop.
Save is8ac/b191d71ad4a3ee0973cd to your computer and use it in GitHub Desktop.
Two jars, one containing three red, five green, and two yellow balls, the other containing five red, one green, and four yellow balls, if a single ball is drawn from each jar, what is the probability that at least one is green?
n <- 2^16
sampleOut <- list()
# Put balls in the two jars.
jar1 <- c("red","red","red","green","green","green","green","green","yellow","yellow")
jar2 <- c("red","red","red","red","red","green","yellow","yellow","yellow","yellow")
# Do the following n times.
for(place in 1:n){
# Take a random ball from each of the two jars
jar1Samp <- sample(jar1,1)
jar2Samp <- sample(jar2,1)
# If at least one of the balls is green, then isGreen is TRUE.
# If not, then it is false.
if (jar1Samp=="green" | jar2Samp=="green") isGreen=TRUE else isGreen=FALSE
sampleOut[place] <- isGreen
}
length(sampleOut[sampleOut==TRUE])/n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment