Random
π² Randomβ
The CSharpNumerics.Statistics.Random namespace provides RandomGenerator β a seedable random number engine with advanced sampling methods built on top of System.Random.
using CSharpNumerics.Statistics.Random;
π± Seedable Constructionβ
Create a reproducible generator by supplying a seed:
var rng = new RandomGenerator(seed: 42);
π― Continuous Distributionsβ
Uniform
double u = rng.NextUniform(2.0, 5.0);
Gaussian (Box-Muller transform)
double g = rng.NextGaussian(mean: 0, standardDeviation: 1);
Exponential (inverse transform sampling)
double e = rng.NextExponential(lambda: 2.0);
π’ Discrete Distributionsβ
Poisson (Knuth / rejection)
int p = rng.NextPoisson(lambda: 4.0);
Bernoulli
int coin = rng.NextBernoulli(0.5);
Binomial
int hits = rng.NextBinomial(n: 20, p: 0.3);
π¦ Batch Samplingβ
Generate large arrays of samples in a single call:
double[] gaussianSamples = rng.GaussianSamples(1000);
double[] uniformSamples = rng.UniformSamples(1000, min: 0, max: 10);
π Shuffle & Sampleβ
Randomly reorder a collection or draw without replacement:
var deck = new[] { 1, 2, 3, 4, 5 };
rng.Shuffle(deck);
var hand = rng.Sample(deck, k: 3);