Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Random span-based API #13708

Merged
merged 3 commits into from
Aug 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions src/mscorlib/shared/System/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,17 @@
// 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
{
//
// 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
//
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove % (Byte.MaxValue + 1)); ? Although, I'm not sure what it was for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think it's redundant, the byte cast is enough. Fine.

}
}

public virtual void NextBytes(Span<byte> buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)Next();
}
}
}
Expand Down