diff --git a/src/Numerics/Random/RandomSource.cs b/src/Numerics/Random/RandomSource.cs index c9089f4c..cce5a1f8 100644 --- a/src/Numerics/Random/RandomSource.cs +++ b/src/Numerics/Random/RandomSource.cs @@ -165,7 +165,7 @@ namespace MathNet.Numerics.Random return Next(); } - // Sample with 1 < maxExclusive < int.MaxValue + // Sample with maxExclusive ≥ 2 if (_threadSafe) { lock (_lock) @@ -214,7 +214,7 @@ namespace MathNet.Numerics.Random return Next(maxExclusive); } - // Sample with minInclusive + 1 < maxExclusive + // Sample with maxExclusive ≥ minExclusive + 2 if (_threadSafe) { lock (_lock) @@ -291,7 +291,7 @@ namespace MathNet.Numerics.Random return; } - // Sample with 1 < maxExclusive < int.MaxValue + // Sample with maxExclusive ≥ 2 if (_threadSafe) { lock (_lock) @@ -361,7 +361,7 @@ namespace MathNet.Numerics.Random return; } - // Sample with minInclusive + 1 < maxExclusive + // Sample with maxExclusive ≥ minExclusive + 2 if (_threadSafe) { lock (_lock) @@ -560,16 +560,9 @@ namespace MathNet.Numerics.Random /// /// Returns a random 32-bit signed integer within the specified range. /// - /// The exclusive upper bound of the random number returned. Range: maxExclusive ≥ 1. + /// The exclusive upper bound of the random number returned. Range: maxExclusive ≥ 2 (not verified, must be ensured by caller). 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 // (biased: return (int)(DoSample() * maxExclusive);) @@ -597,9 +590,10 @@ namespace MathNet.Numerics.Random /// Returns a random 32-bit signed integer within the specified range. /// /// The inclusive lower bound of the random number returned. - /// The exclusive upper bound of the random number returned. Range: maxExclusive > minExclusive. + /// The exclusive upper bound of the random number returned. Range: maxExclusive ≥ minExclusive + 2 (not verified, must be ensured by caller). protected virtual int DoSampleInteger(int minInclusive, int maxExclusive) { + // Sample with maxExclusive ≥ 2 return DoSampleInteger(maxExclusive - minInclusive) + minInclusive; } } diff --git a/src/Numerics/Random/SystemRandomSource.cs b/src/Numerics/Random/SystemRandomSource.cs index 04c0dee0..e7554c3c 100644 --- a/src/Numerics/Random/SystemRandomSource.cs +++ b/src/Numerics/Random/SystemRandomSource.cs @@ -126,6 +126,10 @@ namespace MathNet.Numerics.Random return _random.Next(); } + /// + /// Returns a random 32-bit signed integer within the specified range. + /// + /// The exclusive upper bound of the random number returned. Range: maxExclusive ≥ 2 (not verified, must be ensured by caller). protected override int DoSampleInteger(int maxExclusive) { return _random.Next(maxExclusive); @@ -135,12 +139,15 @@ namespace MathNet.Numerics.Random /// Returns a random 32-bit signed integer within the specified range. /// /// The inclusive lower bound of the random number returned. - /// The exclusive upper bound of the random number returned. must be greater than or equal to . + /// The exclusive upper bound of the random number returned. Range: maxExclusive ≥ minExclusive + 2 (not verified, must be ensured by caller). protected override int DoSampleInteger(int minInclusive, int maxExclusive) { return _random.Next(minInclusive, maxExclusive); } + /// + /// Fills the elements of a specified array of bytes with random numbers in full range, including zero and 255 (). + /// protected override void DoSampleBytes(byte[] buffer) { _random.NextBytes(buffer);