Discussion 8
- Write a program to toss a coin 10,000 times. Let Sn be the number of
heads in the first n tosses. Have your program print out, after every
1000 tosses, Sn − n/2. On the basis of this simulation, is it correct to
say that you can expect heads about half of the time when you toss a
coin a large number of times?
n <- 1000
Sn <- c()
sum_heads <- 0
n_values <- c()
for (i in 1:10) {
simulation <- sample(0:1, n, replace = TRUE) # corrected 'replace' argument
sum_heads <- sum_heads + sum(simulation == 1)
Sn <- c(Sn, sum_heads / (n * i))
n_values <- c(n_values, n * i)
}
data_frame <- data.frame(n_values, Sn)
data_frame
## n_values Sn
## 1 1000 0.5200000
## 2 2000 0.5185000
## 3 3000 0.5136667
## 4 4000 0.5132500
## 5 5000 0.5054000
## 6 6000 0.5060000
## 7 7000 0.5058571
## 8 8000 0.5103750
## 9 9000 0.5090000
## 10 10000 0.5102000