Apache Commons logo Apache Commons RNG

Apache Commons RNG: Random Numbers Generators

Commons RNG provides implementations of pseudo-random numbers generators that are either faster or of higher quality (and sometimes both) than java.util.Random.

The "sampling" module contains classes to generate samples that follow the statistics of a given distribution.


import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;

public class NormalDeviates {
    private final ContinuousSampler normalizedGaussian;

    public NormalDeviates(UniformRandomProvider rng) {
        normalizedGaussian = MarsagliaNormalizedGaussianSampler.of(rng);

    public double sample(double mean,
                         double sigma) {
        return mean + sigma * normalizedGaussian.sample();

Browse the Javadoc for more information.