DARTBOARD PROBLEM
#Dart Board Monte Carlo Simulation
# n:number of darts thrown
dartsim = function(n){
score = rep(0,n)
for (i in 1:n){
rad = runif(1,0,12) #two coordinates per throw, 0:12 for a radius of 12 dartboard
if(rad<.5){
score[i]=50
}
else if (rad>=.5 && rad<1){
score[i]=25
}
else if (rad>=1 && rad<5){
score[i]=sample(1:20,1)
}
else if (rad>=5 && rad<5.5){
score[i]=3*sample(1:20,1)
}
else if (rad>=5.5 && rad<8.5){
score[i]=sample(1:20,1)
}
else if (rad>=8.5 && rad<9){
score[i]=2*sample(1:20,1)
}
else if (rad>=9 && rad<=12){
score[i]=0
}
}
#cant have if using other function
#return(c("total score is:",sum(score)) )
return(sum(score))
}
#Throwing 3 darts
dartsim(3)
## [1] 74
dartrep = function(n,repeats){
totscore = rep(0,repeats)
for (i in 1:repeats){
totscore[i] = dartsim(n)
}
avgscore=mean(totscore)
sdscore=sd(totscore)
return( c("mean:",avgscore,"std dev:",sdscore) )
}
#1000 games
dartrep(3,10000)
## [1] "mean:" "34.4948" "std dev:"
## [4] "22.2007400832951"