A Ruby library for the binomial distribution

View the Project on GitHub kmikael/bernoulli

Bernoulli: Discrete probability distribution library for Ruby

Bernoulli implements the four most common discrete probability distributions in an object oriented manner and in pure Ruby.


Use RubyGems to install bernoulli

$ gem install bernoulli


The classes in bernoulli reprsent random variables:


If, for example you wanted to have a geometrically distributed random variable with parameter 0.43, you would do:

y =

Also if you require 'bernoulli/shorthand' too, you will get top level shortcut functions to create new instances of all distributions:

So y = geomdist(0.43) would be equivalent to the code shown above.

After this you call methods on y. The methods avalible to all distributions are:

Again, shortcuts include (but this time included automatically):


require 'bernoulli'
require 'bernoulli/shorthand'

x = binomdist(180, 1.0/6)
# => #<Distribution::Binomial @n=180, @p=0.16666666666666666>

x.ev # => 30.0
x.v # => 25.0
x[25..36] # => 0.7665588897840108

Last, but not least, there are two methods exclusive to the binomial distribution:

The method sample returns a random array of length n, where each entry is 1 with a probability of p. This is esentially modelling a binomial experiment.

The method sample_value returns the number of successes in a random binomial expriment that was executed using sample. sv is a shortcut for sample_value.


bernoulli is a small project. After writing the same code for some project and then losing it two or three times I decided to do it one time and well, so I can just call in the code from here next time.

Feel free to cantact me about anything I could/should add or to contribute in any way to this simple library.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request