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

Next() public method

Generates a random int over the range 0 to int.MaxValue-1. MaxValue is not generated in order to remain functionally equivalent to System.Random.Next(). This does slightly eat into some of the performance gain over System.Random, but not much. For better performance see: Call NextInt() for an int over the range 0 to int.MaxValue. Call NextUInt() and cast the result to an int to generate an int over the full Int32 value range including negative values.
public Next ( ) : int
return int
        public int Next()
        {
            uint t = _x^(_x<<11);
            _x=_y; _y=_z; _z=_w;
            _w = (_w^(_w>>19))^(t^(t>>8));

            // Handle the special case where the value int.MaxValue is generated. This is outside of
            // the range of permitted values, so we therefore call Next() to try again.
            uint rtn = _w&0x7FFFFFFF;
            if(rtn==0x7FFFFFFF) {
                return Next();
            }
            return (int)rtn;
        }

Same methods

XorShiftRandom::Next ( int upperBound ) : int
XorShiftRandom::Next ( int lowerBound, int upperBound ) : int

Usage Example

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

            for(int i=0; i<sampleCount; i++){
                sampleArr[i] = rng.Next();
            }

            UniformDistributionTest(sampleArr, 0.0, int.MaxValue);
        }
All Usage Examples Of Redzen.Numerics.XorShiftRandom::Next