public float nextPolar ( ) { // If there's a saved value, return it. if ( haveNextPolar ) { haveNextPolar = false ; return nextPolar ; } float u1 , u2 , r ; // point coordinates and their radius do { // u1 and u2 will be uniformly-distributed // random values in [-1, +1). u1 = 2 * gen . nextFloat ( ) - 1 ; u2 = 2 * gen . nextFloat ( ) - 1 ; // Want radius r inside the unit circle. r = u1 * u1 + u2 * u2 ; } while ( r >= 1 ) ; // Factor incorporates the standard deviation. float factor = ( float ) ( stddev * Math . sqrt ( - 2 * Math . log ( r ) / r ) ) ; // v1 and v2 are normally-distributed random values. float v1 = factor * u1 + mean ; float v2 = factor * u2 + mean ; // Save v1 for next time. nextPolar = v1 ; haveNextPolar = true ; return v2 ; }
Compute the next randomn value using the polar algorithm . Requires two uniformly - distributed random values in [ - 1 + 1 ) . Actually computes two random values and saves the second one for the next invokation .