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?
# set.seed(1)
CoinToss <- 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)
}
CoinToss(10000)
## Value of n: 100 Proportions of heads: -0.04
## Value of n: 200 Proportions of heads: -0.06
## Value of n: 300 Proportions of heads: -0.04333333
## Value of n: 400 Proportions of heads: -0.0375
## Value of n: 500 Proportions of heads: -0.03
## Value of n: 600 Proportions of heads: -0.02166667
## Value of n: 700 Proportions of heads: -0.01714286
## Value of n: 800 Proportions of heads: -0.0025
## Value of n: 900 Proportions of heads: 0.004444444
## Value of n: 1000 Proportions of heads: 0.012
## Value of n: 1100 Proportions of heads: 0.006363636
## Value of n: 1200 Proportions of heads: 0.005
## Value of n: 1300 Proportions of heads: 0.003076923
## Value of n: 1400 Proportions of heads: 0.002857143
## Value of n: 1500 Proportions of heads: 0.003333333
## Value of n: 1600 Proportions of heads: 0.00375
## Value of n: 1700 Proportions of heads: 0.002352941
## Value of n: 1800 Proportions of heads: 0.003333333
## Value of n: 1900 Proportions of heads: 0.003157895
## Value of n: 2000 Proportions of heads: 0.0055
## Value of n: 2100 Proportions of heads: 0.004761905
## Value of n: 2200 Proportions of heads: 0.009545455
## Value of n: 2300 Proportions of heads: 0.01130435
## Value of n: 2400 Proportions of heads: 0.01166667
## Value of n: 2500 Proportions of heads: 0.0088
## Value of n: 2600 Proportions of heads: 0.009230769
## Value of n: 2700 Proportions of heads: 0.01037037
## Value of n: 2800 Proportions of heads: 0.008928571
## Value of n: 2900 Proportions of heads: 0.006896552
## Value of n: 3000 Proportions of heads: 0.008333333
## Value of n: 3100 Proportions of heads: 0.007096774
## Value of n: 3200 Proportions of heads: 0.0096875
## Value of n: 3300 Proportions of heads: 0.006969697
## Value of n: 3400 Proportions of heads: 0.005882353
## Value of n: 3500 Proportions of heads: 0.007142857
## Value of n: 3600 Proportions of heads: 0.006666667
## Value of n: 3700 Proportions of heads: 0.006756757
## Value of n: 3800 Proportions of heads: 0.006842105
## Value of n: 3900 Proportions of heads: 0.008974359
## Value of n: 4000 Proportions of heads: 0.00925
## Value of n: 4100 Proportions of heads: 0.007804878
## Value of n: 4200 Proportions of heads: 0.005714286
## Value of n: 4300 Proportions of heads: 0.004418605
## Value of n: 4400 Proportions of heads: 0.002954545
## Value of n: 4500 Proportions of heads: 0.003555556
## Value of n: 4600 Proportions of heads: 0.00326087
## Value of n: 4700 Proportions of heads: 0.002553191
## Value of n: 4800 Proportions of heads: 0.002916667
## Value of n: 4900 Proportions of heads: 0.002040816
## Value of n: 5000 Proportions of heads: 0.0012
## Value of n: 5100 Proportions of heads: 0.001960784
## Value of n: 5200 Proportions of heads: 0.002884615
## Value of n: 5300 Proportions of heads: 0.002641509
## Value of n: 5400 Proportions of heads: 0.0007407407
## Value of n: 5500 Proportions of heads: 0.0003636364
## Value of n: 5600 Proportions of heads: -0.0001785714
## Value of n: 5700 Proportions of heads: -0.0005263158
## Value of n: 5800 Proportions of heads: -0.001206897
## Value of n: 5900 Proportions of heads: -0.002542373
## Value of n: 6000 Proportions of heads: -0.003166667
## Value of n: 6100 Proportions of heads: -0.003114754
## Value of n: 6200 Proportions of heads: -0.003225806
## Value of n: 6300 Proportions of heads: -0.002698413
## Value of n: 6400 Proportions of heads: -0.00203125
## Value of n: 6500 Proportions of heads: -0.001846154
## Value of n: 6600 Proportions of heads: -0.001969697
## Value of n: 6700 Proportions of heads: -0.001940299
## Value of n: 6800 Proportions of heads: -0.003235294
## Value of n: 6900 Proportions of heads: -0.003333333
## Value of n: 7000 Proportions of heads: -0.004428571
## Value of n: 7100 Proportions of heads: -0.005492958
## Value of n: 7200 Proportions of heads: -0.005
## Value of n: 7300 Proportions of heads: -0.005068493
## Value of n: 7400 Proportions of heads: -0.005405405
## Value of n: 7500 Proportions of heads: -0.0056
## Value of n: 7600 Proportions of heads: -0.005789474
## Value of n: 7700 Proportions of heads: -0.005194805
## Value of n: 7800 Proportions of heads: -0.004487179
## Value of n: 7900 Proportions of heads: -0.003164557
## Value of n: 8000 Proportions of heads: -0.00225
## Value of n: 8100 Proportions of heads: -0.001975309
## Value of n: 8200 Proportions of heads: -0.001219512
## Value of n: 8300 Proportions of heads: -0.001686747
## Value of n: 8400 Proportions of heads: -0.001904762
## Value of n: 8500 Proportions of heads: -0.001647059
## Value of n: 8600 Proportions of heads: -0.0002325581
## Value of n: 8700 Proportions of heads: -0.0008045977
## Value of n: 8800 Proportions of heads: -0.001363636
## Value of n: 8900 Proportions of heads: -0.001235955
## Value of n: 9000 Proportions of heads: -0.001333333
## Value of n: 9100 Proportions of heads: -0.001648352
## Value of n: 9200 Proportions of heads: -0.002934783
## Value of n: 9300 Proportions of heads: -0.002473118
## Value of n: 9400 Proportions of heads: -0.003191489
## Value of n: 9500 Proportions of heads: -0.003368421
## Value of n: 9600 Proportions of heads: -0.003229167
## Value of n: 9700 Proportions of heads: -0.003402062
## Value of n: 9800 Proportions of heads: -0.003265306
## Value of n: 9900 Proportions of heads: -0.002626263
## Value of n: 10000 Proportions of heads: -0.0019
##
## [1] "Head "
## [1] 4981
## [1] "Tail "
## [1] 5019
As the value of n increases the numbers approaches to 0.
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?
# set.seed(1)
CoinToss <- 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)
}
CoinToss(10000)
## Value of n: 100 the proportion of heads minus 1/2 -0.05
## the number of heads minus half the number of tosses -5
## Value of n: 200 the proportion of heads minus 1/2 -0.03
## the number of heads minus half the number of tosses -6
## Value of n: 300 the proportion of heads minus 1/2 -0.02666667
## the number of heads minus half the number of tosses -8
## Value of n: 400 the proportion of heads minus 1/2 -0.0125
## the number of heads minus half the number of tosses -5
## Value of n: 500 the proportion of heads minus 1/2 -0.008
## the number of heads minus half the number of tosses -4
## Value of n: 600 the proportion of heads minus 1/2 -0.01166667
## the number of heads minus half the number of tosses -7
## Value of n: 700 the proportion of heads minus 1/2 -0.01714286
## the number of heads minus half the number of tosses -12
## Value of n: 800 the proportion of heads minus 1/2 -0.01625
## the number of heads minus half the number of tosses -13
## Value of n: 900 the proportion of heads minus 1/2 -0.01333333
## the number of heads minus half the number of tosses -12
## Value of n: 1000 the proportion of heads minus 1/2 -0.015
## the number of heads minus half the number of tosses -15
## Value of n: 1100 the proportion of heads minus 1/2 -0.01363636
## the number of heads minus half the number of tosses -15
## Value of n: 1200 the proportion of heads minus 1/2 -0.01083333
## the number of heads minus half the number of tosses -13
## Value of n: 1300 the proportion of heads minus 1/2 -0.005384615
## the number of heads minus half the number of tosses -7
## Value of n: 1400 the proportion of heads minus 1/2 -0.002857143
## the number of heads minus half the number of tosses -4
## Value of n: 1500 the proportion of heads minus 1/2 -0.0006666667
## the number of heads minus half the number of tosses -1
## Value of n: 1600 the proportion of heads minus 1/2 -0.00125
## the number of heads minus half the number of tosses -2
## Value of n: 1700 the proportion of heads minus 1/2 -0.0005882353
## the number of heads minus half the number of tosses -1
## Value of n: 1800 the proportion of heads minus 1/2 -0.002777778
## the number of heads minus half the number of tosses -5
## Value of n: 1900 the proportion of heads minus 1/2 -0.006315789
## the number of heads minus half the number of tosses -12
## Value of n: 2000 the proportion of heads minus 1/2 -0.0055
## the number of heads minus half the number of tosses -11
## 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.003181818
## the number of heads minus half the number of tosses -7
## Value of n: 2300 the proportion of heads minus 1/2 -0.001304348
## the number of heads minus half the number of tosses -3
## Value of n: 2400 the proportion of heads minus 1/2 -0.0004166667
## the number of heads minus half the number of tosses -1
## Value of n: 2500 the proportion of heads minus 1/2 0
## the number of heads minus half the number of tosses 0
## Value of n: 2600 the proportion of heads minus 1/2 -0.003846154
## the number of heads minus half the number of tosses -10
## Value of n: 2700 the proportion of heads minus 1/2 -0.004074074
## the number of heads minus half the number of tosses -11
## Value of n: 2800 the proportion of heads minus 1/2 -0.005714286
## the number of heads minus half the number of tosses -16
## Value of n: 2900 the proportion of heads minus 1/2 -0.004137931
## the number of heads minus half the number of tosses -12
## Value of n: 3000 the proportion of heads minus 1/2 -0.004666667
## the number of heads minus half the number of tosses -14
## Value of n: 3100 the proportion of heads minus 1/2 -0.004193548
## the number of heads minus half the number of tosses -13
## Value of n: 3200 the proportion of heads minus 1/2 -0.0040625
## the number of heads minus half the number of tosses -13
## Value of n: 3300 the proportion of heads minus 1/2 -0.006060606
## the number of heads minus half the number of tosses -20
## Value of n: 3400 the proportion of heads minus 1/2 -0.004705882
## the number of heads minus half the number of tosses -16
## Value of n: 3500 the proportion of heads minus 1/2 -0.006285714
## the number of heads minus half the number of tosses -22
## Value of n: 3600 the proportion of heads minus 1/2 -0.006388889
## the number of heads minus half the number of tosses -23
## Value of n: 3700 the proportion of heads minus 1/2 -0.005135135
## the number of heads minus half the number of tosses -19
## Value of n: 3800 the proportion of heads minus 1/2 -0.003684211
## the number of heads minus half the number of tosses -14
## 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.0035
## the number of heads minus half the number of tosses -14
## Value of n: 4100 the proportion of heads minus 1/2 -0.002682927
## the number of heads minus half the number of tosses -11
## Value of n: 4200 the proportion of heads minus 1/2 -0.001904762
## the number of heads minus half the number of tosses -8
## Value of n: 4300 the proportion of heads minus 1/2 -0.002093023
## the number of heads minus half the number of tosses -9
## Value of n: 4400 the proportion of heads minus 1/2 -0.0004545455
## the number of heads minus half the number of tosses -2
## Value of n: 4500 the proportion of heads minus 1/2 0.0002222222
## the number of heads minus half the number of tosses 1
## Value of n: 4600 the proportion of heads minus 1/2 -0.0006521739
## the number of heads minus half the number of tosses -3
## Value of n: 4700 the proportion of heads minus 1/2 -0.002978723
## the number of heads minus half the number of tosses -14
## 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.00122449
## the number of heads minus half the number of tosses -6
## Value of n: 5000 the proportion of heads minus 1/2 -0.0012
## the number of heads minus half the number of tosses -6
## Value of n: 5100 the proportion of heads minus 1/2 0
## the number of heads minus half the number of tosses 0
## Value of n: 5200 the proportion of heads minus 1/2 0.001153846
## the number of heads minus half the number of tosses 6
## Value of n: 5300 the proportion of heads minus 1/2 0.002075472
## the number of heads minus half the number of tosses 11
## Value of n: 5400 the proportion of heads minus 1/2 0.0009259259
## the number of heads minus half the number of tosses 5
## Value of n: 5500 the proportion of heads minus 1/2 0.002727273
## the number of heads minus half the number of tosses 15
## Value of n: 5600 the proportion of heads minus 1/2 0.0025
## the number of heads minus half the number of tosses 14
## 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.002413793
## the number of heads minus half the number of tosses 14
## Value of n: 5900 the proportion of heads minus 1/2 0.003389831
## the number of heads minus half the number of tosses 20
## Value of n: 6000 the proportion of heads minus 1/2 0.0035
## the number of heads minus half the number of tosses 21
## Value of n: 6100 the proportion of heads minus 1/2 0.003606557
## the number of heads minus half the number of tosses 22
## Value of n: 6200 the proportion of heads minus 1/2 0.003064516
## the number of heads minus half the number of tosses 19
## Value of n: 6300 the proportion of heads minus 1/2 0.003809524
## the number of heads minus half the number of tosses 24
## Value of n: 6400 the proportion of heads minus 1/2 0.00375
## the number of heads minus half the number of tosses 24
## Value of n: 6500 the proportion of heads minus 1/2 0.002307692
## the number of heads minus half the number of tosses 15
## Value of n: 6600 the proportion of heads minus 1/2 0.001666667
## the number of heads minus half the number of tosses 11
## Value of n: 6700 the proportion of heads minus 1/2 0.0008955224
## the number of heads minus half the number of tosses 6
## Value of n: 6800 the proportion of heads minus 1/2 0.0007352941
## the number of heads minus half the number of tosses 5
## Value of n: 6900 the proportion of heads minus 1/2 0.001449275
## the number of heads minus half the number of tosses 10
## Value of n: 7000 the proportion of heads minus 1/2 0.001
## the number of heads minus half the number of tosses 7
## Value of n: 7100 the proportion of heads minus 1/2 0.001408451
## the number of heads minus half the number of tosses 10
## Value of n: 7200 the proportion of heads minus 1/2 0.001111111
## the number of heads minus half the number of tosses 8
## Value of n: 7300 the proportion of heads minus 1/2 0.0004109589
## the number of heads minus half the number of tosses 3
## Value of n: 7400 the proportion of heads minus 1/2 -0.0006756757
## the number of heads minus half the number of tosses -5
## Value of n: 7500 the proportion of heads minus 1/2 -0.0009333333
## the number of heads minus half the number of tosses -7
## Value of n: 7600 the proportion of heads minus 1/2 -0.0006578947
## the number of heads minus half the number of tosses -5
## Value of n: 7700 the proportion of heads minus 1/2 -0.0003896104
## the number of heads minus half the number of tosses -3
## Value of n: 7800 the proportion of heads minus 1/2 -0.0002564103
## the number of heads minus half the number of tosses -2
## Value of n: 7900 the proportion of heads minus 1/2 0.0005063291
## the number of heads minus half the number of tosses 4
## Value of n: 8000 the proportion of heads minus 1/2 0.001
## the number of heads minus half the number of tosses 8
## Value of n: 8100 the proportion of heads minus 1/2 -0.0007407407
## the number of heads minus half the number of tosses -6
## Value of n: 8200 the proportion of heads minus 1/2 -0.0004878049
## the number of heads minus half the number of tosses -4
## Value of n: 8300 the proportion of heads minus 1/2 -0.001807229
## the number of heads minus half the number of tosses -15
## Value of n: 8400 the proportion of heads minus 1/2 -0.002142857
## the number of heads minus half the number of tosses -18
## Value of n: 8500 the proportion of heads minus 1/2 -0.002235294
## the number of heads minus half the number of tosses -19
## Value of n: 8600 the proportion of heads minus 1/2 -0.00244186
## the number of heads minus half the number of tosses -21
## Value of n: 8700 the proportion of heads minus 1/2 -0.002183908
## the number of heads minus half the number of tosses -19
## Value of n: 8800 the proportion of heads minus 1/2 -0.002386364
## the number of heads minus half the number of tosses -21
## Value of n: 8900 the proportion of heads minus 1/2 -0.002696629
## the number of heads minus half the number of tosses -24
## Value of n: 9000 the proportion of heads minus 1/2 -0.003222222
## the number of heads minus half the number of tosses -29
## Value of n: 9100 the proportion of heads minus 1/2 -0.003516484
## the number of heads minus half the number of tosses -32
## Value of n: 9200 the proportion of heads minus 1/2 -0.003804348
## the number of heads minus half the number of tosses -35
## Value of n: 9300 the proportion of heads minus 1/2 -0.004301075
## the number of heads minus half the number of tosses -40
## Value of n: 9400 the proportion of heads minus 1/2 -0.004680851
## the number of heads minus half the number of tosses -44
## Value of n: 9500 the proportion of heads minus 1/2 -0.004842105
## the number of heads minus half the number of tosses -46
## Value of n: 9600 the proportion of heads minus 1/2 -0.004270833
## the number of heads minus half the number of tosses -41
## Value of n: 9700 the proportion of heads minus 1/2 -0.004845361
## the number of heads minus half the number of tosses -47
## Value of n: 9800 the proportion of heads minus 1/2 -0.005204082
## the number of heads minus half the number of tosses -51
## Value of n: 9900 the proportion of heads minus 1/2 -0.006363636
## the number of heads minus half the number of tosses -63
## Value of n: 10000 the proportion of heads minus 1/2 -0.0057
## the number of heads minus half the number of tosses -57
##
## [1] "Head "
## [1] 4943
## [1] "Tail "
## [1] 5057
Yes.These numbers appears to approach 0 as n increases