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
示例#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);
        }
示例#2
0
        public void NextLowerUpper_LongRange_Distribution()
        {
            int sampleCount = 10000000;
            XorShiftRandom rng = new XorShiftRandom();

            int maxValHalf = int.MaxValue / 2;
            int lowerBound = -(maxValHalf + 10000);
            int upperBound = (maxValHalf + 10000);

            // N.B. double precision can represent every Int32 value exactly.
            double[] sampleArr = new double[sampleCount];
            for(int i=0; i<sampleCount; i++) {
                sampleArr[i] = rng.Next(lowerBound, upperBound);
            }

            UniformDistributionTest(sampleArr, lowerBound, upperBound);
        }
示例#3
0
        public void NextLowerUpper_LongRange_Bounds()
        {
            int sampleCount = 10000000;
            XorShiftRandom rng = new XorShiftRandom();
            Random sysRng = new Random();

            int maxValHalf = int.MaxValue / 2;
            double[] sampleArr = new double[sampleCount];

            for(int i=0; i<sampleCount; i++)
            {
                int lowerBound = -(maxValHalf + (sysRng.Next()/2));
                int upperBound = (maxValHalf + (sysRng.Next()/2));
                int sample = rng.Next(lowerBound, upperBound);

                if(sample < lowerBound || sample >= upperBound) {
                    Assert.Fail();
                }
            }
        }
示例#4
0
        public void NextLowerUpper()
        {
            int sampleCount = 10000000;
            XorShiftRandom rng = new XorShiftRandom();
            double[] sampleArr = new double[sampleCount];

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

            UniformDistributionTest(sampleArr, 1000000, 1234567);
        }