#3. 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?

#lets assume that 1 is a head
#and 0 is a tail

#number of tosses
n <- 1000

#create vector for storing proportion and number of tosses
prop <- c()
n <- c()

#craete variable that stores sum of heads
sum_heads <- 0


#iterate 10 times 
for (i in 1:10){

#toss 1000 times
outcome <- sample(0:1,1000,rep=T)
#sum of heads
sum_heads <- sum_heads + sum(outcome==1)
prop <- c(prop,sum_heads/(1000*i))
n <-c(n,1000*i)

}

data <- data.frame(n,prop)
data
##        n      prop
## 1   1000 0.5010000
## 2   2000 0.5030000
## 3   3000 0.5093333
## 4   4000 0.5012500
## 5   5000 0.4990000
## 6   6000 0.4983333
## 7   7000 0.4992857
## 8   8000 0.4980000
## 9   9000 0.5002222
## 10 10000 0.4991000
plot(data,type = "l",main="heads-to-tails ratio experiment", xlab="number of tosses", ylab="proportion of heads")

The plot shows that as the number of tosses increases heads-to-tails ratio becomes closer to 50:50 ratio. This fact proves law of large numbers concept.