Redzen.Numerics.XorShiftRandom.NextDoubleNonZero C# (CSharp) Method

NextDoubleNonZero() public method

Generates a random double. Values returned are over the range (0, 1). That is, exclusive of both 0.0 and 1.0.
public NextDoubleNonZero ( ) : double
return double
        public double NextDoubleNonZero()
        {
            uint t = _x^(_x<<11);
            _x=_y; _y=_z; _z=_w;

            // Here we generate a random value from 0 to 0xff ff ff fe, and add one
            // to generate a random value from 1 to 0xff ff ff ff.
            return REAL_UNIT_UINT * ((0xFFFFFFFE&(_w=(_w^(_w>>19))^(t^(t>>8)))) + 1U);
        }

Usage Example

Example #1
0
        public void NextDoubleNonZero()
        {
            int sampleCount = 10000000;
            XorShiftRandom rng = new XorShiftRandom();
            double[] sampleArr = new double[sampleCount];

            for(int i=0; i<sampleCount; i++)
            {
                sampleArr[i] = rng.NextDoubleNonZero();
                if(0.0 == sampleArr[i]) Assert.Fail();
            }

            UniformDistributionTest(sampleArr, 0.0, 1.0);
        }