【问题标题】:Plot a table of binomial distributions in R在 R 中绘制二项分布表
【发布时间】:2013-08-01 06:09:27
【问题描述】:

对于游戏设计问题,我需要更好地检查二项分布。使用 R,我需要构建一个二维表 - 给定固定参数“池”(掷骰子的数量),“边数”(骰子的边数)具有:

  • 在行中 --> 成功的最小值(从 0 到边,这是一个离散分布)
  • 在列中 --> 成功数(范围从 0 到池)

我知道如何将其作为单个任务进行计算,但我不确定如何迭代以填充整个表格

编辑:我忘了说我要计算至少获得成功次数的概率 p。

【问题讨论】:

  • 看起来这是一个编程问题。如果是这样,这将在 Stack Overflow 上提供更好的服务,并且版主可以为您迁移此问题(标记此帖子,但不要在 SO 上转发)。
  • 向我们展示您的尝试。
  • 一种方法是简单地继续,直到您至少滚动所有值一次

标签: r dice


【解决方案1】:

好的,我认为这可能是一个简单的解决方案。它具有行上的成功率和列上掷骰子 (p) 的成功阈值。

poolDistribution <- function(n, sides=10, digits=2, roll.Under=FALSE){
  m <- 1:sides
  names(m) <- paste(m,ifelse(roll.Under,"-", "+"),sep="")
  s <- 1:n
  names(s) <- paste(s,n,sep="/")
  sapply(m, function(m.value) round((if(roll.Under) (1 - pbinom(s - 1, n, (m.value)/sides))*100 else (1 - pbinom(s - 1, n, (sides - m.value + 1)/sides))*100), digits=digits))  

【讨论】:

    【解决方案2】:

    这可以帮助您完成一半。
    如果您是 R 的新手,您可能会错过一个非常强大的功能,即您可以使用值向量作为另一个向量的索引。这使得部分问题变得非常简单:

    pool <- 3
    sides <- 20  # <cough>D&D<cough>
    
    
    # you need to strore the values somewhere, use a vector
    NumberOfRollsPerSide <- rep(0, sides)
    names(NumberOfRollsPerSide) <- 1:sides # this will be useful in table
    
    ## Repeast so long as there are still zeros
    ##     ie, so long as there is a side that has not come up yet
    while (any(NumberOfRollsPerSide == 0)) {
      # roll once
      oneRoll <- sample(1:sides, pool, TRUE)  
    
      # add (+1) to each sides' total rolls
      #  note that you can use the roll outcome to index the vector. R is great. 
      NumberOfRollsPerSide[oneRoll] <- NumberOfRollsPerSide[oneRoll] + 1
    }
    
    # These are your results: 
      NumberOfRollsPerSide
    

    您现在剩下要做的就是数一数,每边,它首先出现在哪个卷号中。

    【讨论】:

    • ehehe tnx,这不是我的作业。不幸的是,我太老了,不能上大学了 ;) 我们刚刚决定用更严格的统计来增加我们的 RPG cough非基于 D20 的cough 设计活动。我对 R 并不完全陌生,但我通常将它用于分析而不是真正的编程。但是让我现在试试你的代码! :) tnx 为您解答!
    • 虽然这不是我需要计算的(我相应地编辑了问题),但您的代码 sn-p 对于我需要分析的其他内容仍然纯粹有用! Tnx 分配给您的贡献!
    • 如果是D&D,肯定是3d6 AHEM
    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 2020-06-18
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2023-04-08
    • 2019-10-30
    • 2015-01-04
    相关资源
    最近更新 更多