AForge.Math.Random.StandardGenerator.Next C# (CSharp) Method

Next() public method

Generate next random number.
public Next ( ) : double
return double
        public double Next()
        {
            // check if we can use second value
            if (useSecond)
            {
                // return the second number
                useSecond = false;
                return secondValue;
            }

            double x1, x2, w, firstValue;

            // generate new numbers
            do
            {
                x1 = rand.Next()*2.0 - 1.0;
                x2 = rand.Next()*2.0 - 1.0;
                w = x1*x1 + x2*x2;
            } while (w >= 1.0);

            w = System.Math.Sqrt((-2.0*System.Math.Log(w))/w);

            // get two standard random numbers
            firstValue = x1*w;
            secondValue = x2*w;

            useSecond = true;

            // return the first number
            return firstValue;
        }

Usage Example

Example #1
0
 /// <summary>
 /// Generate next random number.
 /// </summary>
 ///
 /// <returns>Returns next random number.</returns>
 ///
 public float Next( )
 {
     return((float)rand.Next( ) * stdDev + mean);
 }
All Usage Examples Of AForge.Math.Random.StandardGenerator::Next