Using R to simulate a biased 6 sided dice

前端 未结 3 342
执笔经年
执笔经年 2021-01-28 12:45

In R I want to figure the code to simulate a biased 6 sided die being thrown 44 times. The die is biased in the sense that the number 6 is twice as likely to be thrown as any ot

相关标签:
3条回答
  • 2021-01-28 13:09

    You need both the replace=TRUE and the prob-argument set to the non-equal probability setting of your choice.

    throws <- sample(1:6, 44, replace=TRUE, prob=c(1,1,1,1,1,2)/7 )
    # Two realizations
    > throws <- sample(1:6, 44, replace=TRUE, prob=c(1,1,1,1,1,2)/7 )
    > table(throws)
    throws
     1  2  3  4  5  6 
    10  5  8  7  3 11 
    > throws <- sample(1:6, 44, replace=TRUE, prob=c(1,1,1,1,1,2)/7 )
    > table(throws)
    throws
     1  2  3  4  5  6 
     7  3  4  8  7 15 
    

    Notice that the outcome is still (pseudo)-random and that it's still possible to have departures from what the naive student of probability might expect. And I cannot resist the pedantic correction that this is for a single die rather than "dice", which is plural.

    0 讨论(0)
  • 2021-01-28 13:14
    sample(1:6, size = 44, replace = TRUE, prob = c(rep(1, 5), 2))
    
    0 讨论(0)
  • 2021-01-28 13:31

    You can use sample and specify probabilities for each number.

      sample(x = 1:6, size = 44, replace = T, prob = c(rep(1/7, 5), 2/7))
    
    0 讨论(0)
提交回复
热议问题