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?
coinTosses <- function(numFlips) {
print(paste0("Initiating ", numFlips, " flips!"))
if (numFlips < 100) {
print("Number of flips should be at least 100")
}
numheads = 0
for (i in 1:numFlips) {
randomnum <- runif(n=1, min=0, max=1)
if (randomnum < 0.5) {
numheads = numheads + 1
}
if (i%%100 == 0 ){
proportionheads = numheads/i
print(paste0("At flip ", i, " the proportion of heads minus 1/2: ", proportionheads-0.5, " and number of heads (", numheads, ") minus half the number of tosses (",i/2,"): ", numheads-(i/2)))
}
}
}
coinTosses(500)
## [1] "Initiating 500 flips!"
## [1] "At flip 100 the proportion of heads minus 1/2: 0.02 and number of heads (52) minus half the number of tosses (50): 2"
## [1] "At flip 200 the proportion of heads minus 1/2: 0.02 and number of heads (104) minus half the number of tosses (100): 4"
## [1] "At flip 300 the proportion of heads minus 1/2: 0.00666666666666671 and number of heads (152) minus half the number of tosses (150): 2"
## [1] "At flip 400 the proportion of heads minus 1/2: 0.0175 and number of heads (207) minus half the number of tosses (200): 7"
## [1] "At flip 500 the proportion of heads minus 1/2: 0.03 and number of heads (265) minus half the number of tosses (250): 15"
coinTosses(1000)
## [1] "Initiating 1000 flips!"
## [1] "At flip 100 the proportion of heads minus 1/2: -0.02 and number of heads (48) minus half the number of tosses (50): -2"
## [1] "At flip 200 the proportion of heads minus 1/2: -0.03 and number of heads (94) minus half the number of tosses (100): -6"
## [1] "At flip 300 the proportion of heads minus 1/2: -0.03 and number of heads (141) minus half the number of tosses (150): -9"
## [1] "At flip 400 the proportion of heads minus 1/2: -0.00750000000000001 and number of heads (197) minus half the number of tosses (200): -3"
## [1] "At flip 500 the proportion of heads minus 1/2: -0.00600000000000001 and number of heads (247) minus half the number of tosses (250): -3"
## [1] "At flip 600 the proportion of heads minus 1/2: 0.0083333333333333 and number of heads (305) minus half the number of tosses (300): 5"
## [1] "At flip 700 the proportion of heads minus 1/2: 0.0171428571428571 and number of heads (362) minus half the number of tosses (350): 12"
## [1] "At flip 800 the proportion of heads minus 1/2: 0.0275 and number of heads (422) minus half the number of tosses (400): 22"
## [1] "At flip 900 the proportion of heads minus 1/2: 0.0277777777777778 and number of heads (475) minus half the number of tosses (450): 25"
## [1] "At flip 1000 the proportion of heads minus 1/2: 0.03 and number of heads (530) minus half the number of tosses (500): 30"
Both values get smaller over time, tending towards 0. I tried this with 10,000 flips but I dont want to clutter the submission!