three dice

dice.sum <- function(dice){
   dice <- sample(1:6, size = dice, replace = TRUE)
   return(sum(dice))
}

三個骰子隨機骰150次的範例

sample(dice.sum(3),150,replace=TRUE)
##   [1]  2 12 14  9  3  9  3 13  9  3 15 11  7 11  3 13  9  4 11 12  9 15 11
##  [24]  8  9  8  2 15 13  3  6 15 15 15 13 12 15 11  9 15  8 12  5  4  3  1
##  [47]  2 12 11  2 10  2 15 15 11  5  2  1  2 10 12 11  3 13  2  7  2  3  5
##  [70] 13  4  1  6  1 14 12  8  5 12 13  2  1  9 10  6 15 12  8  7 12 13 13
##  [93] 12  4  5  4  4  4  2  6  9  6  5  4  5  7 15  9  9 15  6 10 14  4  4
## [116]  4  5 14 13  7  7  8 11  9  7 15 13 10  6  9  1 12 10  2  3  3  9  1
## [139]  5  3 13  9  3  9  7 15  8 14  7  1

定義隨機骰1000次,呈現常態分佈。

dice <- replicate(1000, dice.sum(3))
plot(table(dice),ylab = 'Frequency', main = 'Three dice rolling')

The end.