用R模拟乐透

时间:2014-10-21 09:25:55

标签: r for-loop simulation

制作乐透模拟器的最简单方法是什么?让我们假设我买了10种不同的组合(39种中有7种)。那么如何计算获胜的预期值如果4,5,6,7正确的数字我得到5 $,70 $,6500 $和350000 $。以及如何使用我的10种不同组合来模拟获得某些内容的概率(和情节),例如1到10000个图形。

    set.seed(99)
    tickets<-10
    y<-replicate(tickets,sample(1:39,7,replace=FALSE))
    dimnames(y)<-list(rownames(y,do.NULL=FALSE,prefix=""),
              colnames(y,do.NULL=FALSE,prefix="combination"))
    m<-t(y)
   (m2=t(apply(m,1,sort))) 

                    [,1] [,2] [,3] [,4] [,5] [,6] [,7]
    combinations1     5   19   23   26   33   36   39
    combinations2     7   12   14   18   20   22   37
    combinations3     4    7    8   14   25   27   36
    combinations4     1    4   13   22   27   28   32
    combinations5     1    2    8   12   13   19   37
    combinations6    16   18   22   27   30   31   35
    combinations7    13   15   18   20   31   34   36
    combinations8     5   10   27   28   29   31   35
    combinations9     4   10   14   21   23   33   35
    combinations10    1   17   20   28   29   32   33

1 个答案:

答案 0 :(得分:0)

不是整个答案......而是比较部分

winningNumbers=sample(1:39,7,replace=FALSE)
numberOfNumbersRight=apply(m2,1,function(x)sum(x%in%winningNumbers))

然后你只是循环它们......

for(i in numberOfNumbersRight){
  if(i>5)print("you won a cheap prize")
}
相关问题