Search code examples
rdice

Plot a table of binomial distributions in R


For a game design issue, I need to better inspect binomial distributions. Using R, I need to build a two dimensional table that - given a fixed parameters 'pool' (the number of dice rolled), 'sides' (the number of sides of the die) has:

  • In rows --> minimum for a success (ranging from 0 to sides, it's a discrete distribution)
  • In columns --> number of successes (ranging from 0 to pool)

I know how to calculate it as a single task, but I'm not sure on how to iterate to fill the entire table

EDIT: I forgot to say that I want to calculate the probability p of gaining at least the number of successes.


Solution

  • Ok, i think this could be a simple solution. It has ratio of successes on rows and success thresholds on dice roll (p) on columns.

    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))