Chapter 1.1 Discrete Probability Distributions
CoinTosses <- function(num_of_rolls){
counter <- 1
heads <- 0
tails <- 0
flip_list <- sample(0:1, num_of_rolls, replace = TRUE)
for (i in flip_list){
if(i == 1){
#print("Heads")
heads <- heads + 1
} else{
#print("Tails")
tails <- tails + 1
}
if (counter %% 100 == 0){
print(paste0("Counter: ", counter))
print(paste0("Proportion of heads minus 1/2: ", heads/(heads+tails)-0.5))
}
counter <- counter + 1
}
print(paste0("Total Heads: ", heads))
print(paste0("Total tails: ", tails))
}
CoinTosses(1000)
## [1] "Counter: 100"
## [1] "Proportion of heads minus 1/2: 0.07"
## [1] "Counter: 200"
## [1] "Proportion of heads minus 1/2: 0.035"
## [1] "Counter: 300"
## [1] "Proportion of heads minus 1/2: 0.03"
## [1] "Counter: 400"
## [1] "Proportion of heads minus 1/2: 0.0275"
## [1] "Counter: 500"
## [1] "Proportion of heads minus 1/2: 0.018"
## [1] "Counter: 600"
## [1] "Proportion of heads minus 1/2: 0.005"
## [1] "Counter: 700"
## [1] "Proportion of heads minus 1/2: 0.0128571428571429"
## [1] "Counter: 800"
## [1] "Proportion of heads minus 1/2: 0.015"
## [1] "Counter: 900"
## [1] "Proportion of heads minus 1/2: 0.0144444444444445"
## [1] "Counter: 1000"
## [1] "Proportion of heads minus 1/2: 0.013"
## [1] "Total Heads: 513"
## [1] "Total tails: 487"
The numbers do appear to be getting closer to 0 as the n increases.
roulette <- function(num_of_spins){
dollars <- 0 # Assume that zero is the baseline
spins <- sample(1:38, num_of_spins, replace = TRUE)
for (count in spins){
if(count <= 18){
# There are 18 red slots out of a total of 38 possibilities
dollars <- dollars + 1
} else {
dollars <- dollars - 1
}
}
print(paste0("Total winnings: $", dollars))
}
# Let's run the program 10 times
for (i in 1:10){
roulette(1000)
}
## [1] "Total winnings: $-122"
## [1] "Total winnings: $-54"
## [1] "Total winnings: $-66"
## [1] "Total winnings: $-48"
## [1] "Total winnings: $-26"
## [1] "Total winnings: $-14"
## [1] "Total winnings: $-20"
## [1] "Total winnings: $-42"
## [1] "Total winnings: $-6"
## [1] "Total winnings: $-126"
It looks like Roulette may not be the best game to play after all.