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.
halfcounter = 0
thirdcounter = 0
sixthcounter = 0
for (i in 1:1000) {
spin = runif(1)
if(spin < .5) {halfcounter = halfcounter + 1}
if(spin >= .5 & spin <= (.5+(1/3))) {thirdcounter = thirdcounter + 1}
if(spin > (.5+(1/3))){sixthcounter = sixthcounter + 1}
}
tab <- matrix(c(halfcounter/1000,thirdcounter/1000,sixthcounter/1000,1/2,1/3,1/6),ncol=3, byrow = T)
barplot(tab, main="Simulation vs Expected",
xlab="spinner", col=c("darkblue","red"),
beside=TRUE)