Files
2026-05-27 23:47:53 +01:00

2.3 KiB
Raw Permalink Blame History

distributions/continuous

(no submodules)

Purpose

The continuous module implements the nine continuous probability distributions most commonly used in simulations, analytics, and game mechanics: Normal (Gaussian), LogNormal, Exponential, Gamma, Beta, Pareto, Weibull, Cauchy and Logistic. Each is a stateless function taking parameters and an optional rng?; together they cover the overwhelming majority of non-uniform sampling needs.

Features

  • normal(mean, stddev, rng?) — Box-Muller transform
  • logNormal(mu, sigma, rng?) — exponential of a Normal
  • exponential(lambda, rng?) — inverse-CDF method
  • gamma(shape, scale, rng?) — Marsaglia–Tsang for α ≥ 1, Ahrens-Dieter boost for α < 1
  • beta(alpha, beta, rng?) — two Gamma draws
  • pareto(xm, alpha, rng?) — inverse-CDF
  • weibull(k, lambda, rng?) — inverse-CDF
  • cauchy(x0, gamma, rng?) — tangent-transform
  • logistic(mu, s, rng?) — inverse-CDF
  • Every function validates positivity of scale parameters and throws RandomError.DISTRIBUTION.INVALID_PARAM

Usage: Basic Example

import { normal, exponential } from "@suaveplan/random/distributions";

const z = normal(0, 1);
const wait = exponential(2);

Usage: Advanced Example

import { Sfc32Rng } from "@suaveplan/pseudo-random";
import { gamma, beta } from "@suaveplan/random/distributions";

const rng = new Sfc32Rng("mc-sim-42");
const serviceTime = gamma(2, 3, rng);
const conversionRate = beta(8, 92, rng); // ~8% with informative prior

API Reference

All functions: (...params, rng?): number. Parameter positivity is validated; violations raise RandomError.DISTRIBUTION.INVALID_PARAM. Default parameter values match the standard form of each distribution (e.g. normal() returns a standard Normal).

Implementation Notes

  • Box-Muller discards the paired draw; this is the simpler variant of the transform and has no measurable performance cost at JS call scales.
  • The Marsaglia–Tsang Gamma sampler is the fastest acceptance-rejection method known for α ≥ 1. For α < 1 we use the boost trick gamma(α) = gamma(α+1) · U^(1/α).
  • Beta uses two Gamma draws from the same source, which leaves the mean and variance exactly right without requiring a specialised sampler.