Java – How to generate Gaussian Noise in Java


I am looking for a Gaussian Noise generator that takes in 2 parameters: mean and variance, and then generates the Gaussian Noise.

During the searching, I find quite a number of such generators for images. However, what I want is a generic generator. I wish to do something like:

myGaussianNoiseGenerator = new GaussianNoiseGenerator(mean, variance);
mySignalWithNoise = mySignal + myGaussianNoiseGenerator.generate();

How may I do this?

Best Solution

De-normalizing the output of Random.nextGaussian() to your needs should be straightforward:

java.util.Random r = new java.util.Random();
double noise = r.nextGaussian() * Math.sqrt(variance) + mean;