Search code examples
javanormal-distribution

Equivalent method in java (numpy.random.normal(mean,var))


Is there any method in java that is equivalent to numpy.random.normal(mean,variance).


Solution

  • You can use the java.util.Random class:

    Random r = new Random();
    double randomValue = mean + r.nextGaussian()*std_dev;
    

    Note that if you need multiple random values, you can use r multiple times. You can also supply the constructor with a specific seed Random r = new Random(1234);.