Distributions

DistributionUsage
Normal DistributionUsed to model real world, as many real world processes are normal distributed, like height of people and many other natural processes.
Roulette DistributionThis distribution can be used to generate a custom random process where specific choices (slots) should have custom occurences. Like choice A should have 50% probability, choice B 25% and choice C 25%.

Normal Distribution

Normal (Marsaglia)

The methods NormalDistributionMarsaglia.NextNormal(ref <RNG>, mean, std) are using the Marsaglia polar method to generate a random number following the normal distribution.

Normal (Ziggurat)

The methods NormalDistributionZiggurat.NextNormal(ref <RNG>, mean, std) are using the Ziggurat algorithm which is usually faster than the Marsaglia method, even when the implementation is much more complicated. It also needs additional memory, as lookup tables are needed. In contrast to the marsaglia method, Ziggurat mostly doesn’t use any complicated math function like sin, cos or exp.

Roulette

The roulette distribution chooses a slot randomly between 0 and length (excluding). Each slot has its own probability. Probabilities can be specified in uint, float and double.

The individual probabilities are calculated as individual probability / sum over all values.

Using uints can sometimes be more accurate/faster, as float division or multiplication is avoided.

Example usage:

var prob = new NativeArray<uint>(101, Allocator.Temp);

// initialize probabilities
for (var i = 0; i < prob.Length; i++) { 
    prob[i] = 1;
}

// Generate weights for faster RNG generation
var weights = RouletteDistribution.GenerateWeights(prob, Allocator.Temp);

// Sample the distribution
var val = RouletteDistribution.NextRoulette(ref rng, weights);

The individual probability is 1/100 ( = the sum of all values ).