I’ve been working a lot lately with random numbers. I have developed a random number generator that uses the normal distribution curve as the basis for the random numbers. So it creates random numbers that are normal xD.
Here is the source in different languages:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static double gaussianRand () { double u1, u2, w, g; double mean = 1; // mu or mean in Normal Distribution double stdDist = 0.18f; // sigma or variance in Normal Distribution Random rand = new Random(); do { u1 = 2 * rand.nextFloat() - 1; u2 = 2 * rand.nextFloat() - 1; w = u1 * u1 + u2 * u2; } while ( w >= 1 ); w = Math.sqrt( ( -2 * Math.log10( w ) ) / w ); g = u2 * w; return g * stdDist + mean; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function gaussianRand ( mean, stdDist ) { var u1, u2, w, g; mean = ( mean ) ? mean : 1; // mu or mean in Normal Distribution stdDist = ( stdDist ) ? stdDist : 0.18; // sigma or variance in Normal Distribution do { u1 = 2 * Math.random() - 1; u2 = 2 * Math.random() - 1; w = u1 * u1 + u2 * u2; } while ( w >= 1 ); w = Math.sqrt( ( -2 * ( Math.log( w ) / Math.log( 10 ) ) ) / w ); g = u2 * w; return g * stdDist + mean; } |