Page 12 Excercise 1

Modify the program CoinTosses to toss a coin n times and print out after every 100 tosses the proportion of heads minus 1/2. Do these numbers appear to approach 0 as n increases? Modify the program again to print out, every 100 times, both of the following quantities: the proportion of heads minus 1/2, and the number of heads minus half the number of tosses. Do these numbers appear to approach 0 as n increases?

These numbers do appear to approach 0 as n increases. The second time I modify the program the number changes but still appear to approach 0 as n increases. See below.

CoinTosses1 <- function(n){
    coin <- c("heads","tails")
    toss <- sample(coin, size = n, replace = TRUE)
    count_heads <- length(which(toss == "heads"))
    x <- count_heads/n - 0.5
   
    return(x)
   
}
CoinTosses1(100)
## [1] 0.06
CoinTosses2 <- function(n){
    coin <- c("heads","tails")
    toss <- sample(coin, size = n, replace = TRUE)
    count_heads <- length(which(toss == "heads"))
    x <- count_heads/n - 0.5
   
    return(c(x, count_heads - n/2))
   
}
CoinTosses2(100)
## [1] -0.05 -5.00

Reference: https://www.gastonsanchez.com/packyourcode/intro.html