random_variates_crystal

A Crystal shard with generators for popular distributions, including Uniform, Triangle, Exponential, Normal, Gamma, Poisson, Geometric, and Binomial."

random_variates

This library implements random variate generation for several common statistical distributions.

Each distribution is implemented in its own class, and different parameterizations are created as instances of the class using the constructor to specify the parameterization. All constructors use named parameters for clarity, so the order of parameters does not matter. All RandomVariate classes provide an optional argument rng, with which the user can specify a U(0,1) that is a subclass of Random to use as the core source of randomness. If rng is not specified, it defaults to Random. The Random class is extended to implement a next method, making it (and alternative random number generators written as subclasses) iterable.

Once a random variate class has been instantiated, values can either be generated on demand using the next method or by using the instance as a generator in any iterable context.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      random_variates:
        github: your-github-user/random_variates
    
  2. Run shards install

Usage

require "random_variates"

tri = RandomVariates::Triangle.new(min: -5.0, max: 5.0, mode: 3.0)
pois = RandomVariates::Poisson.new(rate: 2.0)
exp = RandomVariates::Exponential.new(mean: 42)  # specify either mean or rate

# generate one-by-one with next
5.times { puts "#{tri.next},#{pois.next},#{exp.next}" }

puts # to get a separator blank line

# generate an array with 3 standard normals
puts RandomVariates::Normal.new.first(3).to_a

Generator classes are available for Uniform, Triangle, Exponential, Normal, Gamma, Poisson, Geometric, and Binomial distributions. Details are available in the docs or the source code.

Contributors

Repository

random_variates_crystal

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 2 hours ago
  • June 7, 2026
License

MIT License

Links
Synced at

Sun, 07 Jun 2026 23:10:56 GMT

Languages