public float nextPolar ( ) { // If there's a saved value, return it. if ( m_bHaveNextPolar ) { m_bHaveNextPolar = false ; return m_fNextPolar ; } // point coordinates and their radius float u1 , u2 , r ; do { // u1 and u2 will be uniformly-distributed // random values in [-1, +1). u1 = 2 * GENERATOR . nextFloat ( ) - 1 ; u2 = 2 * GENERATOR . nextFloat ( ) - 1 ; // Want radius r inside the unit circle. r = u1 * u1 + u2 * u2 ; } while ( r >= 1 ) ; // Factor incorporates the standard deviation. final float factor = ( float ) ( m_fStddev * Math . sqrt ( - 2 * Math . log ( r ) / r ) ) ; // v1 and v2 are normally-distributed random values. final float v1 = factor * u1 + m_fMean ; final float v2 = factor * u2 + m_fMean ; // Save v1 for next time. m_fNextPolar = v1 ; m_bHaveNextPolar = 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 .