Beispiel #1
0
        /// <summary>
        /// Take a sample from the standard gaussian distribution, i.e. with mean of 0 and standard deviation of 1.
        /// </summary>
        public double SampleStandard()
        {
            for (;;)
            {
                // Select box at random.
                byte   u    = _rng.NextByte();
                int    i    = (int)(u & 0x7F);
                double sign = ((u & 0x80) == 0) ? -1.0 : 1.0;

                // Generate uniform random value with range [0,0xffffffff].
                uint u2 = _rng.NextUInt();

                // Special case for the base segment.
                if (0 == i)
                {
                    if (u2 < _xComp[0])
                    {   // Generated x is within R0.
                        return(u2 * __UIntToU * _A_Div_Y0 * sign);
                    }
                    // Generated x is in the tail of the distribution.
                    return(SampleTail() * sign);
                }

                // All other segments.
                if (u2 < _xComp[i])
                {   // Generated x is within the rectangle.
                    return(u2 * __UIntToU * _x[i] * sign);
                }

                // Generated x is outside of the rectangle.
                // Generate a random y coordinate and test if our (x,y) is within the distribution curve.
                // This execution path is relatively slow/expensive (makes a call to Math.Exp()) but relatively rarely executed,
                // although more often than the 'tail' path (above).
                double x = u2 * __UIntToU * _x[i];
                if (_y[i - 1] + ((_y[i] - _y[i - 1]) * _rng.NextDouble()) < GaussianPdfDenorm(x))
                {
                    return(x * sign);
                }
            }
        }