diff --git a/src/mscorlib/shared/System/Random.cs b/src/mscorlib/shared/System/Random.cs index 4affed8a1a0d..20f035e2e82e 100644 --- a/src/mscorlib/shared/System/Random.cs +++ b/src/mscorlib/shared/System/Random.cs @@ -2,20 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -/*============================================================ -** -** -** Purpose: A random number generator. -** -** -===========================================================*/ - -using System; -using System.Runtime; -using System.Runtime.CompilerServices; -using System.Globalization; -using System.Diagnostics.Contracts; - namespace System { public class Random @@ -23,11 +9,10 @@ public class Random // // Private Constants // - private const int MBIG = Int32.MaxValue; + private const int MBIG = int.MaxValue; private const int MSEED = 161803398; private const int MZ = 0; - // // Member Variables // @@ -64,7 +49,7 @@ public Random(int Seed) int mj, mk; //Initialize our Seed array. - int subtraction = (Seed == Int32.MinValue) ? Int32.MaxValue : Math.Abs(Seed); + int subtraction = (Seed == int.MinValue) ? int.MaxValue : Math.Abs(Seed); mj = MSEED - subtraction; _seedArray[55] = mj; mk = 1; @@ -195,8 +180,8 @@ private double GetSampleForLargeRange() result = -result; } double d = result; - d += (Int32.MaxValue - 1); // get a number in range [0 .. 2 * Int32MaxValue - 1) - d /= 2 * (uint)Int32.MaxValue - 1; + d += (int.MaxValue - 1); // get a number in range [0 .. 2 * Int32MaxValue - 1) + d /= 2 * (uint)int.MaxValue - 1; return d; } @@ -213,10 +198,9 @@ public virtual int Next(int minValue, int maxValue) { throw new ArgumentOutOfRangeException(nameof(minValue), SR.Format(SR.Argument_MinMaxValue, nameof(minValue), nameof(maxValue))); } - Contract.EndContractBlock(); long range = (long)maxValue - minValue; - if (range <= (long)Int32.MaxValue) + if (range <= int.MaxValue) { return ((int)(Sample() * range) + minValue); } @@ -238,7 +222,6 @@ public virtual int Next(int maxValue) { throw new ArgumentOutOfRangeException(nameof(maxValue), SR.Format(SR.ArgumentOutOfRange_MustBePositive, nameof(maxValue))); } - Contract.EndContractBlock(); return (int)(Sample() * maxValue); } @@ -263,10 +246,17 @@ public virtual double NextDouble() public virtual void NextBytes(byte[] buffer) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); - Contract.EndContractBlock(); for (int i = 0; i < buffer.Length; i++) { - buffer[i] = (byte)(InternalSample() % (Byte.MaxValue + 1)); + buffer[i] = (byte)InternalSample(); + } + } + + public virtual void NextBytes(Span buffer) + { + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte)Next(); } } }