RouletteSpin <- function(num.spins){
# function to simulate roulette spins
# ARGS: number of spins
# RETURNS: result of each spin
outcomes <- data.frame(number = c('00','0',
as.character(1:36)),
color=c('green','green','red','black','red','black',
'red','black','red','black','red','black',
'black','red','black','red','black',
'red','black','red','red','black',
'red','black','red','black','red',
'black','red','black','black','red',
'black','red','black','red','black','red'))
return(outcomes[sample(38,num.spins,replace=T),])
}
kable(RouletteSpin(2), row.names=F)
| number | color |
|---|---|
| 15 | black |
| 30 | red |
Calculate/Derive the probability of landing on green, red, and black.
How can the RouletteSpin() function be used to compute or approximate these probabilities?
Now what happens if we:
run the simulation again with the same number of trials?
run the simulation with more trials, say 1 million?
We have touched on many of these before, but here are some examples of expressions (conditions) in R.
pi > 3 & pi < 3.5
c(1,3,5,7) %in% 1:3
1:3 %in% c(1,3,5,7)
rand.uniform <- runif(n = 1, min = 0, max = 1)
rand.uniform < .5
Write a conditional statement that takes a playing card with two arguments:
card.number <- '4' and card.suit <- 'C' = 4 of clubs or card.number <-'K' and card.suit <- 'H' = King of hearts) andYes if the card is a red face card and No otherwise.Verify this works using the following inputs:
card.number <- 'J' and card.suit <- 'D'card.number <- 'Q' and card.suit <- 'S'Assume you plan to wager $1 on red for ten roulette spins. If the outcome is red you win a dollar and otherwise you lose a dollar. Write a loop that simulates ten rolls and determines your net profit or loss.
#hint: to get color from a single spin use
RouletteSpin(1)[2]
## color
## 7 red