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.BoxMullerGaussianSampler;

public class NormalDeviates {
   private final ContinuousSampler normalizedGaussian;

    public NormalDeviates(UniformRandomProvider rng) {
        normalizedGaussian = new BoxMullerGaussianSampler(rng, 0, 1);

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

Browse the Javadoc for more information.