Exercise 1 page 22

a. 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?

b. 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?

a.

set.seed(101)

CoinTosses <- function(n){
      headTotal <- 0
    tailTotal <- 0
for(i in 1:n){
  
    randomNumber <- runif(1,0,1)
if(randomNumber<0.5){
    outcome <- "H"
    headTotal <- headTotal + 1
}else{
    outcome <-"T"
    tailTotal <- tailTotal + 1
}
    # cat(outcome)
    if(i%%100 == 0){
        cat("Value of n: ",i," Proportions of heads:",(headTotal/i)-0.5,"\n")
 
    }
}
   cat("\n")
   print("Head ")
   print(headTotal)
   print("Tail ")
   print(tailTotal)
}
CoinTosses(10000)
## Value of n:  100  Proportions of heads: -0.04 
## Value of n:  200  Proportions of heads: -0.025 
## Value of n:  300  Proportions of heads: 0.01666667 
## Value of n:  400  Proportions of heads: 0.01 
## Value of n:  500  Proportions of heads: 0 
## Value of n:  600  Proportions of heads: 0.005 
## Value of n:  700  Proportions of heads: 0.01 
## Value of n:  800  Proportions of heads: 0.0125 
## Value of n:  900  Proportions of heads: 0.01666667 
## Value of n:  1000  Proportions of heads: 0.021 
## Value of n:  1100  Proportions of heads: 0.01454545 
## Value of n:  1200  Proportions of heads: 0.01166667 
## Value of n:  1300  Proportions of heads: 0.01384615 
## Value of n:  1400  Proportions of heads: 0.003571429 
## Value of n:  1500  Proportions of heads: 0.006 
## Value of n:  1600  Proportions of heads: 0.0075 
## Value of n:  1700  Proportions of heads: 0.007647059 
## Value of n:  1800  Proportions of heads: 0.002222222 
## Value of n:  1900  Proportions of heads: 0.005263158 
## Value of n:  2000  Proportions of heads: 0 
## Value of n:  2100  Proportions of heads: 0.001428571 
## Value of n:  2200  Proportions of heads: 0.0004545455 
## Value of n:  2300  Proportions of heads: 0.00173913 
## Value of n:  2400  Proportions of heads: 0.001666667 
## Value of n:  2500  Proportions of heads: 0.0032 
## Value of n:  2600  Proportions of heads: 0.003461538 
## Value of n:  2700  Proportions of heads: 0.002222222 
## Value of n:  2800  Proportions of heads: 0.001428571 
## Value of n:  2900  Proportions of heads: 0 
## Value of n:  3000  Proportions of heads: -0.001 
## Value of n:  3100  Proportions of heads: 0 
## Value of n:  3200  Proportions of heads: 0.001875 
## Value of n:  3300  Proportions of heads: 0.001212121 
## Value of n:  3400  Proportions of heads: 0.001470588 
## Value of n:  3500  Proportions of heads: 0.0008571429 
## Value of n:  3600  Proportions of heads: 0.003055556 
## Value of n:  3700  Proportions of heads: 0.004054054 
## Value of n:  3800  Proportions of heads: 0.005789474 
## Value of n:  3900  Proportions of heads: 0.005128205 
## Value of n:  4000  Proportions of heads: 0.00425 
## Value of n:  4100  Proportions of heads: 0.004390244 
## Value of n:  4200  Proportions of heads: 0.003333333 
## Value of n:  4300  Proportions of heads: 0.002325581 
## Value of n:  4400  Proportions of heads: 0.003181818 
## Value of n:  4500  Proportions of heads: 0.002 
## Value of n:  4600  Proportions of heads: 0.001521739 
## Value of n:  4700  Proportions of heads: 0.002553191 
## Value of n:  4800  Proportions of heads: 0.001041667 
## Value of n:  4900  Proportions of heads: 0.0008163265 
## Value of n:  5000  Proportions of heads: -2e-04 
## Value of n:  5100  Proportions of heads: -0.0007843137 
## Value of n:  5200  Proportions of heads: -0.001346154 
## Value of n:  5300  Proportions of heads: 0 
## Value of n:  5400  Proportions of heads: 0.002777778 
## Value of n:  5500  Proportions of heads: 0.003090909 
## Value of n:  5600  Proportions of heads: 0.003035714 
## Value of n:  5700  Proportions of heads: 0.002280702 
## Value of n:  5800  Proportions of heads: 0.00137931 
## Value of n:  5900  Proportions of heads: 0.001355932 
## Value of n:  6000  Proportions of heads: 0.002333333 
## Value of n:  6100  Proportions of heads: 0.001967213 
## Value of n:  6200  Proportions of heads: 0.001612903 
## Value of n:  6300  Proportions of heads: 0.0003174603 
## Value of n:  6400  Proportions of heads: -0.0003125 
## Value of n:  6500  Proportions of heads: -0.0001538462 
## Value of n:  6600  Proportions of heads: 0.0006060606 
## Value of n:  6700  Proportions of heads: 0.0005970149 
## Value of n:  6800  Proportions of heads: 0.0004411765 
## Value of n:  6900  Proportions of heads: 0.001304348 
## Value of n:  7000  Proportions of heads: 0.001857143 
## Value of n:  7100  Proportions of heads: 0.0007042254 
## Value of n:  7200  Proportions of heads: 0.0002777778 
## Value of n:  7300  Proportions of heads: 0.0008219178 
## Value of n:  7400  Proportions of heads: 0.001216216 
## Value of n:  7500  Proportions of heads: 0.001866667 
## Value of n:  7600  Proportions of heads: 0.001578947 
## Value of n:  7700  Proportions of heads: 0.0009090909 
## Value of n:  7800  Proportions of heads: 0.001410256 
## Value of n:  7900  Proportions of heads: 0.001265823 
## Value of n:  8000  Proportions of heads: 0.001875 
## Value of n:  8100  Proportions of heads: 0.001604938 
## Value of n:  8200  Proportions of heads: 0.001707317 
## Value of n:  8300  Proportions of heads: 0.002409639 
## Value of n:  8400  Proportions of heads: 0.002380952 
## Value of n:  8500  Proportions of heads: 0.002705882 
## Value of n:  8600  Proportions of heads: 0.002790698 
## Value of n:  8700  Proportions of heads: 0.001609195 
## Value of n:  8800  Proportions of heads: 0.002045455 
## Value of n:  8900  Proportions of heads: 0.00247191 
## Value of n:  9000  Proportions of heads: 0.002111111 
## Value of n:  9100  Proportions of heads: 0.002417582 
## Value of n:  9200  Proportions of heads: 0.001630435 
## Value of n:  9300  Proportions of heads: 0.001075269 
## Value of n:  9400  Proportions of heads: 0.001808511 
## Value of n:  9500  Proportions of heads: 0.001789474 
## Value of n:  9600  Proportions of heads: 0.001666667 
## Value of n:  9700  Proportions of heads: 0.002474227 
## Value of n:  9800  Proportions of heads: 0.00255102 
## Value of n:  9900  Proportions of heads: 0.002929293 
## Value of n:  10000  Proportions of heads: 0.0028 
## 
## [1] "Head "
## [1] 5028
## [1] "Tail "
## [1] 4972

b.

set.seed(101)
CoinTosses <- function(n){
      headTotal <- 0
    tailTotal <- 0
for(i in 1:n){
  
    randomNumber <- runif(1,0,1)
if(randomNumber<0.5){
    outcome <- "H"
    headTotal <- headTotal + 1
}else{
    outcome <-"T"
    tailTotal <- tailTotal + 1
}
    # cat(outcome)
    if(i%%100 == 0){
        cat("Value of n: ",i," the proportion of heads minus 1/2 ",(headTotal/i)-0.5,"\n")
                cat(" the number of heads minus half the number of tosses",headTotal-i/2,"\n")

 
    }
}
   cat("\n")
   print("Head ")
   print(headTotal)
   print("Tail ")
   print(tailTotal)
}
CoinTosses(10000)
## Value of n:  100  the proportion of heads minus 1/2  -0.04 
##  the number of heads minus half the number of tosses -4 
## Value of n:  200  the proportion of heads minus 1/2  -0.025 
##  the number of heads minus half the number of tosses -5 
## Value of n:  300  the proportion of heads minus 1/2  0.01666667 
##  the number of heads minus half the number of tosses 5 
## Value of n:  400  the proportion of heads minus 1/2  0.01 
##  the number of heads minus half the number of tosses 4 
## Value of n:  500  the proportion of heads minus 1/2  0 
##  the number of heads minus half the number of tosses 0 
## Value of n:  600  the proportion of heads minus 1/2  0.005 
##  the number of heads minus half the number of tosses 3 
## Value of n:  700  the proportion of heads minus 1/2  0.01 
##  the number of heads minus half the number of tosses 7 
## Value of n:  800  the proportion of heads minus 1/2  0.0125 
##  the number of heads minus half the number of tosses 10 
## Value of n:  900  the proportion of heads minus 1/2  0.01666667 
##  the number of heads minus half the number of tosses 15 
## Value of n:  1000  the proportion of heads minus 1/2  0.021 
##  the number of heads minus half the number of tosses 21 
## Value of n:  1100  the proportion of heads minus 1/2  0.01454545 
##  the number of heads minus half the number of tosses 16 
## Value of n:  1200  the proportion of heads minus 1/2  0.01166667 
##  the number of heads minus half the number of tosses 14 
## Value of n:  1300  the proportion of heads minus 1/2  0.01384615 
##  the number of heads minus half the number of tosses 18 
## Value of n:  1400  the proportion of heads minus 1/2  0.003571429 
##  the number of heads minus half the number of tosses 5 
## Value of n:  1500  the proportion of heads minus 1/2  0.006 
##  the number of heads minus half the number of tosses 9 
## Value of n:  1600  the proportion of heads minus 1/2  0.0075 
##  the number of heads minus half the number of tosses 12 
## Value of n:  1700  the proportion of heads minus 1/2  0.007647059 
##  the number of heads minus half the number of tosses 13 
## Value of n:  1800  the proportion of heads minus 1/2  0.002222222 
##  the number of heads minus half the number of tosses 4 
## Value of n:  1900  the proportion of heads minus 1/2  0.005263158 
##  the number of heads minus half the number of tosses 10 
## Value of n:  2000  the proportion of heads minus 1/2  0 
##  the number of heads minus half the number of tosses 0 
## Value of n:  2100  the proportion of heads minus 1/2  0.001428571 
##  the number of heads minus half the number of tosses 3 
## Value of n:  2200  the proportion of heads minus 1/2  0.0004545455 
##  the number of heads minus half the number of tosses 1 
## Value of n:  2300  the proportion of heads minus 1/2  0.00173913 
##  the number of heads minus half the number of tosses 4 
## Value of n:  2400  the proportion of heads minus 1/2  0.001666667 
##  the number of heads minus half the number of tosses 4 
## Value of n:  2500  the proportion of heads minus 1/2  0.0032 
##  the number of heads minus half the number of tosses 8 
## Value of n:  2600  the proportion of heads minus 1/2  0.003461538 
##  the number of heads minus half the number of tosses 9 
## Value of n:  2700  the proportion of heads minus 1/2  0.002222222 
##  the number of heads minus half the number of tosses 6 
## Value of n:  2800  the proportion of heads minus 1/2  0.001428571 
##  the number of heads minus half the number of tosses 4 
## Value of n:  2900  the proportion of heads minus 1/2  0 
##  the number of heads minus half the number of tosses 0 
## Value of n:  3000  the proportion of heads minus 1/2  -0.001 
##  the number of heads minus half the number of tosses -3 
## Value of n:  3100  the proportion of heads minus 1/2  0 
##  the number of heads minus half the number of tosses 0 
## Value of n:  3200  the proportion of heads minus 1/2  0.001875 
##  the number of heads minus half the number of tosses 6 
## Value of n:  3300  the proportion of heads minus 1/2  0.001212121 
##  the number of heads minus half the number of tosses 4 
## Value of n:  3400  the proportion of heads minus 1/2  0.001470588 
##  the number of heads minus half the number of tosses 5 
## Value of n:  3500  the proportion of heads minus 1/2  0.0008571429 
##  the number of heads minus half the number of tosses 3 
## Value of n:  3600  the proportion of heads minus 1/2  0.003055556 
##  the number of heads minus half the number of tosses 11 
## Value of n:  3700  the proportion of heads minus 1/2  0.004054054 
##  the number of heads minus half the number of tosses 15 
## Value of n:  3800  the proportion of heads minus 1/2  0.005789474 
##  the number of heads minus half the number of tosses 22 
## Value of n:  3900  the proportion of heads minus 1/2  0.005128205 
##  the number of heads minus half the number of tosses 20 
## Value of n:  4000  the proportion of heads minus 1/2  0.00425 
##  the number of heads minus half the number of tosses 17 
## Value of n:  4100  the proportion of heads minus 1/2  0.004390244 
##  the number of heads minus half the number of tosses 18 
## Value of n:  4200  the proportion of heads minus 1/2  0.003333333 
##  the number of heads minus half the number of tosses 14 
## Value of n:  4300  the proportion of heads minus 1/2  0.002325581 
##  the number of heads minus half the number of tosses 10 
## Value of n:  4400  the proportion of heads minus 1/2  0.003181818 
##  the number of heads minus half the number of tosses 14 
## Value of n:  4500  the proportion of heads minus 1/2  0.002 
##  the number of heads minus half the number of tosses 9 
## Value of n:  4600  the proportion of heads minus 1/2  0.001521739 
##  the number of heads minus half the number of tosses 7 
## Value of n:  4700  the proportion of heads minus 1/2  0.002553191 
##  the number of heads minus half the number of tosses 12 
## Value of n:  4800  the proportion of heads minus 1/2  0.001041667 
##  the number of heads minus half the number of tosses 5 
## Value of n:  4900  the proportion of heads minus 1/2  0.0008163265 
##  the number of heads minus half the number of tosses 4 
## Value of n:  5000  the proportion of heads minus 1/2  -2e-04 
##  the number of heads minus half the number of tosses -1 
## Value of n:  5100  the proportion of heads minus 1/2  -0.0007843137 
##  the number of heads minus half the number of tosses -4 
## Value of n:  5200  the proportion of heads minus 1/2  -0.001346154 
##  the number of heads minus half the number of tosses -7 
## Value of n:  5300  the proportion of heads minus 1/2  0 
##  the number of heads minus half the number of tosses 0 
## Value of n:  5400  the proportion of heads minus 1/2  0.002777778 
##  the number of heads minus half the number of tosses 15 
## Value of n:  5500  the proportion of heads minus 1/2  0.003090909 
##  the number of heads minus half the number of tosses 17 
## Value of n:  5600  the proportion of heads minus 1/2  0.003035714 
##  the number of heads minus half the number of tosses 17 
## Value of n:  5700  the proportion of heads minus 1/2  0.002280702 
##  the number of heads minus half the number of tosses 13 
## Value of n:  5800  the proportion of heads minus 1/2  0.00137931 
##  the number of heads minus half the number of tosses 8 
## Value of n:  5900  the proportion of heads minus 1/2  0.001355932 
##  the number of heads minus half the number of tosses 8 
## Value of n:  6000  the proportion of heads minus 1/2  0.002333333 
##  the number of heads minus half the number of tosses 14 
## Value of n:  6100  the proportion of heads minus 1/2  0.001967213 
##  the number of heads minus half the number of tosses 12 
## Value of n:  6200  the proportion of heads minus 1/2  0.001612903 
##  the number of heads minus half the number of tosses 10 
## Value of n:  6300  the proportion of heads minus 1/2  0.0003174603 
##  the number of heads minus half the number of tosses 2 
## Value of n:  6400  the proportion of heads minus 1/2  -0.0003125 
##  the number of heads minus half the number of tosses -2 
## Value of n:  6500  the proportion of heads minus 1/2  -0.0001538462 
##  the number of heads minus half the number of tosses -1 
## Value of n:  6600  the proportion of heads minus 1/2  0.0006060606 
##  the number of heads minus half the number of tosses 4 
## Value of n:  6700  the proportion of heads minus 1/2  0.0005970149 
##  the number of heads minus half the number of tosses 4 
## Value of n:  6800  the proportion of heads minus 1/2  0.0004411765 
##  the number of heads minus half the number of tosses 3 
## Value of n:  6900  the proportion of heads minus 1/2  0.001304348 
##  the number of heads minus half the number of tosses 9 
## Value of n:  7000  the proportion of heads minus 1/2  0.001857143 
##  the number of heads minus half the number of tosses 13 
## Value of n:  7100  the proportion of heads minus 1/2  0.0007042254 
##  the number of heads minus half the number of tosses 5 
## Value of n:  7200  the proportion of heads minus 1/2  0.0002777778 
##  the number of heads minus half the number of tosses 2 
## Value of n:  7300  the proportion of heads minus 1/2  0.0008219178 
##  the number of heads minus half the number of tosses 6 
## Value of n:  7400  the proportion of heads minus 1/2  0.001216216 
##  the number of heads minus half the number of tosses 9 
## Value of n:  7500  the proportion of heads minus 1/2  0.001866667 
##  the number of heads minus half the number of tosses 14 
## Value of n:  7600  the proportion of heads minus 1/2  0.001578947 
##  the number of heads minus half the number of tosses 12 
## Value of n:  7700  the proportion of heads minus 1/2  0.0009090909 
##  the number of heads minus half the number of tosses 7 
## Value of n:  7800  the proportion of heads minus 1/2  0.001410256 
##  the number of heads minus half the number of tosses 11 
## Value of n:  7900  the proportion of heads minus 1/2  0.001265823 
##  the number of heads minus half the number of tosses 10 
## Value of n:  8000  the proportion of heads minus 1/2  0.001875 
##  the number of heads minus half the number of tosses 15 
## Value of n:  8100  the proportion of heads minus 1/2  0.001604938 
##  the number of heads minus half the number of tosses 13 
## Value of n:  8200  the proportion of heads minus 1/2  0.001707317 
##  the number of heads minus half the number of tosses 14 
## Value of n:  8300  the proportion of heads minus 1/2  0.002409639 
##  the number of heads minus half the number of tosses 20 
## Value of n:  8400  the proportion of heads minus 1/2  0.002380952 
##  the number of heads minus half the number of tosses 20 
## Value of n:  8500  the proportion of heads minus 1/2  0.002705882 
##  the number of heads minus half the number of tosses 23 
## Value of n:  8600  the proportion of heads minus 1/2  0.002790698 
##  the number of heads minus half the number of tosses 24 
## Value of n:  8700  the proportion of heads minus 1/2  0.001609195 
##  the number of heads minus half the number of tosses 14 
## Value of n:  8800  the proportion of heads minus 1/2  0.002045455 
##  the number of heads minus half the number of tosses 18 
## Value of n:  8900  the proportion of heads minus 1/2  0.00247191 
##  the number of heads minus half the number of tosses 22 
## Value of n:  9000  the proportion of heads minus 1/2  0.002111111 
##  the number of heads minus half the number of tosses 19 
## Value of n:  9100  the proportion of heads minus 1/2  0.002417582 
##  the number of heads minus half the number of tosses 22 
## Value of n:  9200  the proportion of heads minus 1/2  0.001630435 
##  the number of heads minus half the number of tosses 15 
## Value of n:  9300  the proportion of heads minus 1/2  0.001075269 
##  the number of heads minus half the number of tosses 10 
## Value of n:  9400  the proportion of heads minus 1/2  0.001808511 
##  the number of heads minus half the number of tosses 17 
## Value of n:  9500  the proportion of heads minus 1/2  0.001789474 
##  the number of heads minus half the number of tosses 17 
## Value of n:  9600  the proportion of heads minus 1/2  0.001666667 
##  the number of heads minus half the number of tosses 16 
## Value of n:  9700  the proportion of heads minus 1/2  0.002474227 
##  the number of heads minus half the number of tosses 24 
## Value of n:  9800  the proportion of heads minus 1/2  0.00255102 
##  the number of heads minus half the number of tosses 25 
## Value of n:  9900  the proportion of heads minus 1/2  0.002929293 
##  the number of heads minus half the number of tosses 29 
## Value of n:  10000  the proportion of heads minus 1/2  0.0028 
##  the number of heads minus half the number of tosses 28 
## 
## [1] "Head "
## [1] 5028
## [1] "Tail "
## [1] 4972