Skip to Content
DocumentationProbability Distributions

Probability Distributions

Simloop includes common probability distributions as composable factory functions. Each factory takes a rng: () => number source and the distribution parameters, and returns a () => number sampler.

Inside handlers, use ctx.dist which pre-binds ctx.random() to all distribution factories:

sim.on('customer:arrive', (event, ctx) => { const nextArrival = ctx.dist.exponential(0.5)(); const serviceTime = ctx.dist.gaussian(10, 2)(); ctx.schedule('customer:arrive', ctx.clock + nextArrival, { ... }); });

Standalone Usage

Factory functions are also exported for use outside of handlers or with a custom RNG:

import { exponential, SeededRandom } from 'simloop'; const rng = new SeededRandom(42); const sampler = exponential(() => rng.next(), 0.5); console.log(sampler()); // sample from exponential

Available Distributions

DistributionFactoryDescription
Uniformuniform(rng, a, b)Continuous on [a, b)
Gaussiangaussian(rng, mean?, stddev?)Normal via Box-Muller (default: standard normal)
Exponentialexponential(rng, rate)Rate λ, mean = 1/λ
Poissonpoisson(rng, lambda)Non-negative integers, mean = λ
Bernoullibernoulli(rng, p)Returns 1 with probability p, 0 otherwise
Zipfzipf(rng, n, s)Ranks [1, n], probability ∝ 1/k^s
Triangulartriangular(rng, min, mode, max)Three-point estimate; min/mode/max
Weibullweibull(rng, scale, shape)Reliability and failure analysis
Lognormallognormal(rng, mu?, sigma?)Right-skewed; service times, response times
Erlangerlang(rng, k, rate)Sum of k exponentials; k-stage sequential processes
Geometricgeometric(rng, p)Trials until first success; minimum value is 1

All factories validate their parameters and throw RangeError for invalid inputs (e.g., negative rate, p outside [0, 1]).

Last updated on