Introduction to Probability, Grinstead, C. Snell, J., 1997
Page 52
Exercise 1

Question
In the spinner problem (see Example 2.1) divide the unit circumference into three arcs of length 1/2, 1/3, and 1/6. Write a program to simulate the spinner experiment 1000 times and print out what fraction of the outcomes fall in each of the three arcs. Now plot a bar graph whose bars have width 1/2, 1/3, and 1/6, and areas equal to the corresponding fractions as determined by your simulation. Show that the heights of the bars are all nearly the same.

Answer

#initiate variable
half=0
third=0
sixth=0

#loop for 1000 spins
for (i in 1:1000) {
  
  result = runif(1)

  if (result<0.5) {half = half + 1} 
  if (result>0.5 & result<0.5+0.3333) {third=third +1} 
  if (result>0.5+0.3333 & result<=1.0) {sixth=sixth +1} 
}


#place results in matrix along with expected probabilities
A<-matrix(c(half/1000, third/1000, sixth/1000, 0.5 ,0.333 , 0.166), nrow = 2, ncol = 3, byrow=TRUE )

#Plot
blue <- rgb(0, 0, 1, alpha = 0.50)
green <- rgb(0, 1, 0, alpha = 0.50)


bp<-barplot(A, main="Simulation vs Expected",
        xlab="Circumference", col=c(blue, green),
        beside=TRUE, names.arg=c("1/2", "1/3", "1/6"))

print(A)
##       [,1]  [,2]  [,3]
## [1,] 0.481 0.355 0.164
## [2,] 0.500 0.333 0.166