public void Next10M() { for (int i = 0; i < __loops; i++) { _rng.Next(); } }
/// <summary> /// Return a random integer in the range [min,max] /// </summary> /// <param name="min">Inclusive minimum of range</param> /// <param name="max">Inclusive maximum of range</param> /// <returns></returns> public int Inclusive(int min, int max) { if (max < int.MaxValue) { max++; } return(_fastRandom.Next(min, max)); }
/// <summary> /// Initialise agent and prey positions. The prey is positioned randomly with at least 4 empty squares between it and a wall (in all directions). /// The agent is positioned randomly but such that the prey is within sensor range (distance 2 or less). /// </summary> public void InitPositions() { // Random pos at least 4 units away from any wall. _preyPos._x = 4 + _rng.Next(_gridSize - 8); _preyPos._y = 4 + _rng.Next(_gridSize - 8); // Agent position. Within range of the prey. double t = 2.0 * Math.PI * _rng.NextDouble(); // Random angle. double r = 2.0 + _rng.NextDouble() * 2.0; // Distance between 2 and 4. _agentPos._x = _preyPos._x + (int)Math.Floor(Math.Cos(t) * r); _agentPos._y = _preyPos._y + (int)Math.Floor(Math.Sin(t) * r); }
/// <summary> /// Randomly shuffles items within a list. /// </summary> /// <param name="list">The list to shuffle.</param> /// <param name="rng">Random number generator.</param> public static void Shuffle <T>(IList <T> list, XorShiftRandom rng) { // This approach was suggested by Jon Skeet in a dotNet newsgroup post and // is also the technique used by the OpenJDK. The use of rnd.Next(i+1) introduces // the possibility of swapping an item with itself, I suspect the reasoning behind this // has to do with ensuring the probability of each possible permutation is approximately equal. for (int i = list.Count - 1; i > 0; i--) { int swapIndex = rng.Next(i + 1); T tmp = list[swapIndex]; list[swapIndex] = list[i]; list[i] = tmp; } }
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); }
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); }
/// <summary> /// Randomly shuffles a sub-span of items within a list. /// </summary> /// <param name="list">The list to shuffle.</param> /// <param name="rng">Random number generator.</param> /// <param name="startIdx">The index of the first item in the segment.</param> /// <param name="endIdx">The index of the last item in the segment, i.e. endIdx is inclusive; the item at endIdx will participate in the shuffle.</param> public static void Shuffle <T>(IList <T> list, XorShiftRandom rng, int startIdx, int endIdx) { // Determine how many items in the list will be being shuffled int itemCount = (endIdx - startIdx); // This approach was suggested by Jon Skeet in a dotNet newsgroup post and // is also the technique used by the OpenJDK. The use of rnd.Next(i+1) introduces // the possibility of swapping an item with itself, I suspect the reasoning behind this // has to do with ensuring the probability of each possible permutation is approximately equal. for (int i = endIdx; i > startIdx; i--) { int swapIndex = startIdx + rng.Next((i - startIdx) + 1); T tmp = list[swapIndex]; list[swapIndex] = list[i]; list[i] = tmp; } }
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); }
public void NextLowerUpper_LongRange_Bounds() { int sampleCount = 10000000; XorShiftRandom rng = new XorShiftRandom(); System.Random sysRng = new System.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(); } } }
private IntPoint[] GenerateRandomTestCase(int largeBoxRelativePos) { // Randomly select a position for the small box (the box is a single pixel in size). IntPoint smallBoxPos = new IntPoint(_rng.Next(TestFieldResolution), _rng.Next(TestFieldResolution)); // Large box center is 5 pixels to the right, down or diagonally from the small box. IntPoint largeBoxPos = smallBoxPos; switch (largeBoxRelativePos) { case 0: // Right largeBoxPos._x += 5; break; case 1: // Down largeBoxPos._y += 5; break; case 2: // Diagonal // Two alternate position get us to exactly 5 pixels distant from the small box. if (_rng.NextBool()) { largeBoxPos._x += 3; largeBoxPos._y += 4; } else { largeBoxPos._x += 4; largeBoxPos._y += 3; } break; } // Handle cases where the large box is outside the visual field or overlapping the edge. if (largeBoxPos._x > CoordBoundIdx) { // Wrap around. largeBoxPos._x -= TestFieldResolution; if (0 == largeBoxPos._x) { // Move box fully into the visual field. largeBoxPos._x++; } } else if (CoordBoundIdx == largeBoxPos._x) { // Move box fully into the visual field. largeBoxPos._x--; } else if (0 == largeBoxPos._x) { // Move box fully into the visual field. largeBoxPos._x++; } if (largeBoxPos._y > CoordBoundIdx) { // Wrap around. largeBoxPos._y -= TestFieldResolution; if (0 == largeBoxPos._y) { // Move box fully into the visual field. largeBoxPos._y++; } } else if (CoordBoundIdx == largeBoxPos._y) { // Move box fully into the visual field. largeBoxPos._y--; } else if (0 == largeBoxPos._y) { // Move box fully into the visual field. largeBoxPos._y++; } return(new IntPoint[] { smallBoxPos, largeBoxPos }); }
private void PerformMutationOp() { int outcome = _opDistribution.Sample(); switch (outcome) { case 0: // Write. { PerformMutationOp_Write(); break; } case 1: // Write byte. { byte b = (byte)_rng.Next(); _strmA.WriteByte(b); _strmB.WriteByte(b); Debug.WriteLine("WriteByte"); break; } case 2: // Change read/write head position. { PerformMutationOp_Position(); break; } case 3: // SetLength { PerformMutationOp_SetLength(); break; } case 4: // Seek { PerformMutationOp_Seek(); break; } case 5: // Trim { _strmB.Trim(); Debug.WriteLine("Trim"); break; } case 6: // Read byte. { int a = _strmA.ReadByte(); int b = _strmB.ReadByte(); if (a != b) { throw new Exception("ReadByte mismatch"); } Debug.WriteLine("ReadByte"); break; } case 7: // Read { int len = _rng.Next(20000); byte[] abuf = new byte[len]; byte[] bbuf = new byte[len]; int alen = _strmA.Read(abuf, 0, len); int blen = _strmB.Read(bbuf, 0, len); if (alen != blen) { throw new Exception("Read mismatch"); } if (!Utils.AreEqual(abuf, bbuf)) { throw new Exception("Read mismatch"); } Debug.WriteLine("Read"); break; } } }