Browse Source

Random: drop redundant range check in inner loop implementation #427

netstandard
Christoph Ruegg 10 years ago
parent
commit
5b236aee76
  1. 20
      src/Numerics/Random/RandomSource.cs
  2. 9
      src/Numerics/Random/SystemRandomSource.cs

20
src/Numerics/Random/RandomSource.cs

@ -165,7 +165,7 @@ namespace MathNet.Numerics.Random
return Next(); return Next();
} }
// Sample with 1 < maxExclusive < int.MaxValue // Sample with maxExclusive ≥ 2
if (_threadSafe) if (_threadSafe)
{ {
lock (_lock) lock (_lock)
@ -214,7 +214,7 @@ namespace MathNet.Numerics.Random
return Next(maxExclusive); return Next(maxExclusive);
} }
// Sample with minInclusive + 1 < maxExclusive // Sample with maxExclusive ≥ minExclusive + 2
if (_threadSafe) if (_threadSafe)
{ {
lock (_lock) lock (_lock)
@ -291,7 +291,7 @@ namespace MathNet.Numerics.Random
return; return;
} }
// Sample with 1 < maxExclusive < int.MaxValue // Sample with maxExclusive ≥ 2
if (_threadSafe) if (_threadSafe)
{ {
lock (_lock) lock (_lock)
@ -361,7 +361,7 @@ namespace MathNet.Numerics.Random
return; return;
} }
// Sample with minInclusive + 1 < maxExclusive // Sample with maxExclusive ≥ minExclusive + 2
if (_threadSafe) if (_threadSafe)
{ {
lock (_lock) lock (_lock)
@ -560,16 +560,9 @@ namespace MathNet.Numerics.Random
/// <summary> /// <summary>
/// Returns a random 32-bit signed integer within the specified range. /// Returns a random 32-bit signed integer within the specified range.
/// </summary> /// </summary>
/// <param name="maxExclusive">The exclusive upper bound of the random number returned. Range: maxExclusive ≥ 1.</param> /// <param name="maxExclusive">The exclusive upper bound of the random number returned. Range: maxExclusive ≥ 2 (not verified, must be ensured by caller).</param>
protected virtual int DoSampleInteger(int maxExclusive) protected virtual int DoSampleInteger(int maxExclusive)
{ {
// Fast case: Only a single number is allowed to be returned
// No random call is needed
if (maxExclusive == 1)
{
return 0;
}
// non-biased implementation // non-biased implementation
// (biased: return (int)(DoSample() * maxExclusive);) // (biased: return (int)(DoSample() * maxExclusive);)
@ -597,9 +590,10 @@ namespace MathNet.Numerics.Random
/// Returns a random 32-bit signed integer within the specified range. /// Returns a random 32-bit signed integer within the specified range.
/// </summary> /// </summary>
/// <param name="minInclusive">The inclusive lower bound of the random number returned.</param> /// <param name="minInclusive">The inclusive lower bound of the random number returned.</param>
/// <param name="maxExclusive">The exclusive upper bound of the random number returned. Range: maxExclusive > minExclusive.</param> /// <param name="maxExclusive">The exclusive upper bound of the random number returned. Range: maxExclusive ≥ minExclusive + 2 (not verified, must be ensured by caller).</param>
protected virtual int DoSampleInteger(int minInclusive, int maxExclusive) protected virtual int DoSampleInteger(int minInclusive, int maxExclusive)
{ {
// Sample with maxExclusive ≥ 2
return DoSampleInteger(maxExclusive - minInclusive) + minInclusive; return DoSampleInteger(maxExclusive - minInclusive) + minInclusive;
} }
} }

9
src/Numerics/Random/SystemRandomSource.cs

@ -126,6 +126,10 @@ namespace MathNet.Numerics.Random
return _random.Next(); return _random.Next();
} }
/// <summary>
/// Returns a random 32-bit signed integer within the specified range.
/// </summary>
/// <param name="maxExclusive">The exclusive upper bound of the random number returned. Range: maxExclusive ≥ 2 (not verified, must be ensured by caller).</param>
protected override int DoSampleInteger(int maxExclusive) protected override int DoSampleInteger(int maxExclusive)
{ {
return _random.Next(maxExclusive); return _random.Next(maxExclusive);
@ -135,12 +139,15 @@ namespace MathNet.Numerics.Random
/// Returns a random 32-bit signed integer within the specified range. /// Returns a random 32-bit signed integer within the specified range.
/// </summary> /// </summary>
/// <param name="minInclusive">The inclusive lower bound of the random number returned.</param> /// <param name="minInclusive">The inclusive lower bound of the random number returned.</param>
/// <param name="maxExclusive">The exclusive upper bound of the random number returned. <paramref name="maxExclusive"/> must be greater than or equal to <paramref name="minInclusive"/>.</param> /// <param name="maxExclusive">The exclusive upper bound of the random number returned. Range: maxExclusive ≥ minExclusive + 2 (not verified, must be ensured by caller).</param>
protected override int DoSampleInteger(int minInclusive, int maxExclusive) protected override int DoSampleInteger(int minInclusive, int maxExclusive)
{ {
return _random.Next(minInclusive, maxExclusive); return _random.Next(minInclusive, maxExclusive);
} }
/// <summary>
/// Fills the elements of a specified array of bytes with random numbers in full range, including zero and 255 (<see cref="F:System.Byte.MaxValue"/>).
/// </summary>
protected override void DoSampleBytes(byte[] buffer) protected override void DoSampleBytes(byte[] buffer)
{ {
_random.NextBytes(buffer); _random.NextBytes(buffer);

Loading…
Cancel
Save