Say we are paying a game of chance: We have a 20-sided die, and we need to roll a value of 10 or greater to pass.
set.seed(1595) # set the seed to make sure your results are replicable
# create a 20-sided die where each value 1 through 20 has an equal chance of being selected
D20 <- c(1:20)
# sample 1 value to simulate rolling the die once
roll1 <- sample(D20,1, replace=T)
roll1
## [1] 15
roll1 >=10
## [1] TRUE
On our first roll, we get a value of 15, which is greater than 10, so we pass.
Now we want to know how many times we would be expected to pass with a value of 10 or greater out of 15,000 rolls.
# create an empty vector to store the results of our 15,000 rolls
pass <- rep(NA, 15000)
# use a loop to sample 15,000 times - to represent 15,000 individual rolls
for(i in 1:length(pass)){
pass[i] <- sample(D20,1, replace=T)
}
# calculate the proportion of time that we pass
length(pass[pass>=10])/15000
## [1] 0.5507333
If we were to roll 15,000 times, we could expect to pass around 55% of the time.
What if we had a modifier of 2, meaning that we could add a value of 2 to each roll? How often could we expect to pass on average?
set.seed(1999)
D20 <- c(1:20)
roll2 <- sample(D20,1, replace=T)
# add our modifier of 2
value <- roll2 + 2
value
## [1] 6
value >= 10
## [1] FALSE
# we pass on our first roll with a final value of 10 (after adding our modifier of 2)
# create an empty vector to store the results of our 15,000 rolls
pass <- rep(NA, 15000)
# use a loop to sample 15,000 times - to represent 15,000 individual rolls
for(i in 1:length(pass)){
pass[i] <- sample(D20,1, replace=T) + 2
}
#Calculate the proportion of time that we pass
length(pass[pass>=10])/15000
## [1] 0.6482667
If we were to roll the D20 10,000 times and add 2 to the value of each roll, we could expect to pass around 65% of the time.
What if we still add a modifier of 2, but we roll twice on each turn and take the lower of the two rolls? How often could we expect to pass on average?
set.seed(29)
D20 <- c(1:20)
# sample 2 values to simulate 2 rolls
rolls <- sample(D20,2, replace=T)
rolls
## [1] 5 12
# select the lower of the two values
roll3 <- min(rolls)
# add our modifier of 2
value2 <- roll3 + 2
value2
## [1] 7
value2 >= 10
## [1] FALSE
# we fail on our first roll with a final value of 7 (after selecting the lower number of 5 and adding our modifier of 2)
# create an empty vector to store the results of our 15,000 rolls
pass <- rep(NA, 15000)
# use a loop to sample 15,000 times - to represent 15,000 individual rolls
for(i in 1:length(pass)){
champ <- sample(D20,2, replace=T)
pass[i] <- min(champ) + 2
}
#Calculate the proportion of time that we pass
length(pass[pass>=10])/15000
## [1] 0.4166
If we were to roll the D20 twice 10,000 times and add 2 to the lower of the two values on each roll, we could expect to pass around 42% of the time.