Skip to content

Instantly share code, notes, and snippets.

@AkselA
Last active June 20, 2019 15:14
Show Gist options
  • Save AkselA/42aa4067ffce2cf630ae9560620e6563 to your computer and use it in GitHub Desktop.
Save AkselA/42aa4067ffce2cf630ae9560620e6563 to your computer and use it in GitHub Desktop.
sides <- 8
lim <- 6
n <- 20
lb <- 12
ub <- 16
pbinom(ub, n, (sides-lim)/sides) - pbinom(lb-1, n, (sides-lim)/sides)
# 0.000935362
fc <- function(x) { # Factorial
x[x == 0] <- 1
vapply(x, function(y) prod(1:y), numeric(1))
}
ch <- function(n, k) fc(n)/(fc(k)*fc(n-k)) # Binomial coefficient
db <- function(x, n, p) ch(n, x) * p^x * (1-p)^(n-x) # PMF
pb <- function(q, n, p) sum(db(0:floor(q), n, p)) # CDF
pb(ub, n, (sides-lim)/sides) - pb(lb-1, n, (sides-lim)/sides)
# 0.000935362
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment