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.
Using ctx.dist (recommended)
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 exponentialAvailable Distributions
| Distribution | Factory | Description |
|---|---|---|
| Uniform | uniform(rng, a, b) | Continuous on [a, b) |
| Gaussian | gaussian(rng, mean?, stddev?) | Normal via Box-Muller (default: standard normal) |
| Exponential | exponential(rng, rate) | Rate λ, mean = 1/λ |
| Poisson | poisson(rng, lambda) | Non-negative integers, mean = λ |
| Bernoulli | bernoulli(rng, p) | Returns 1 with probability p, 0 otherwise |
| Zipf | zipf(rng, n, s) | Ranks [1, n], probability ∝ 1/k^s |
| Triangular | triangular(rng, min, mode, max) | Three-point estimate; min/mode/max |
| Weibull | weibull(rng, scale, shape) | Reliability and failure analysis |
| Lognormal | lognormal(rng, mu?, sigma?) | Right-skewed; service times, response times |
| Erlang | erlang(rng, k, rate) | Sum of k exponentials; k-stage sequential processes |
| Geometric | geometric(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