Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Random span-based API (dotnet/coreclr#13708)
Browse files Browse the repository at this point in the history
* Span NextBytes

* int fixes

* undo ints

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
  • Loading branch information
MaggieTsang authored and jkotas committed Sep 1, 2017
1 parent 2a1a76b commit 17939a1
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions src/System.Private.CoreLib/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();
}
}

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

0 comments on commit 17939a1

Please sign in to comment.