diff --git a/src/Numerics/Random/RandomExtensions.cs b/src/Numerics/Random/RandomExtensions.cs
index 57c5dea9..bd361ead 100644
--- a/src/Numerics/Random/RandomExtensions.cs
+++ b/src/Numerics/Random/RandomExtensions.cs
@@ -122,7 +122,31 @@ namespace MathNet.Numerics.Random
}
///
- /// Fills an array with uniform random numbers greater than or equal to 0.0 and less than 1.0.
+ /// Fills an array with uniform random 32-bit signed integers greater than or equal to zero and less than .
+ ///
+ /// The random number generator.
+ /// The array to fill with random values.
+ ///
+ /// This extension is thread-safe if and only if called on an random number
+ /// generator provided by Math.NET Numerics or derived from the RandomSource class.
+ ///
+ public static void NextInt32s(this System.Random rnd, int[] values)
+ {
+ var rs = rnd as RandomSource;
+ if (rs != null)
+ {
+ rs.NextInt32s(values);
+ return;
+ }
+
+ for (var i = 0; i < values.Length; i++)
+ {
+ values[i] = rnd.Next();
+ }
+ }
+
+ ///
+ /// Fills an array with uniform random 32-bit signed integers within the specified range.
///
/// The random number generator.
/// The array to fill with random values.
diff --git a/src/Numerics/Random/RandomSource.cs b/src/Numerics/Random/RandomSource.cs
index bdb87086..230dd21e 100644
--- a/src/Numerics/Random/RandomSource.cs
+++ b/src/Numerics/Random/RandomSource.cs
@@ -90,6 +90,17 @@ namespace MathNet.Numerics.Random
}
}
+ ///
+ /// Returns an array of uniform random numbers greater than or equal to 0.0 and less than 1.0.
+ ///
+ /// The size of the array to fill.
+ public double[] NextDoubles(int count)
+ {
+ var values = new double[count];
+ NextDoubles(values);
+ return values;
+ }
+
///
/// Returns an infinite sequence of uniform random numbers greater than or equal to 0.0 and less than 1.0.
///
@@ -112,11 +123,8 @@ namespace MathNet.Numerics.Random
}
///
- /// Returns a nonnegative random number.
+ /// Returns a random 32-bit signed integer greater than or equal to zero and less than .
///
- ///
- /// A 32-bit signed integer greater than or equal to zero and less than .
- ///
public sealed override int Next()
{
if (_threadSafe)
@@ -133,17 +141,17 @@ namespace MathNet.Numerics.Random
///
/// Returns a random number less then a specified maximum.
///
- /// The exclusive upper bound of the random number returned.
- /// A 32-bit signed integer less than .
- /// is negative.
- public sealed override int Next(int maxValue)
+ /// The exclusive upper bound of the random number returned.
+ /// A 32-bit signed integer less than .
+ /// is negative.
+ public sealed override int Next(int maxExclusive)
{
- if (maxValue <= 0)
+ if (maxExclusive <= 0)
{
throw new ArgumentException(Resources.ArgumentMustBePositive);
}
- if (maxValue == int.MaxValue)
+ if (maxExclusive == int.MaxValue)
{
return Next();
}
@@ -152,11 +160,11 @@ namespace MathNet.Numerics.Random
{
lock (_lock)
{
- return DoSampleInteger(0, maxValue);
+ return DoSampleInteger(0, maxExclusive);
}
}
- return DoSampleInteger(0, maxValue);
+ return DoSampleInteger(0, maxExclusive);
}
///
@@ -175,6 +183,11 @@ namespace MathNet.Numerics.Random
throw new ArgumentException(Resources.ArgumentMinValueGreaterThanMaxValue);
}
+ if (maxExclusive == int.MaxValue && minInclusive == 0)
+ {
+ return Next();
+ }
+
if (_threadSafe)
{
lock (_lock)
@@ -186,6 +199,42 @@ namespace MathNet.Numerics.Random
return DoSampleInteger(minInclusive, maxExclusive);
}
+ ///
+ /// Fills an array with random 32-bit signed integers greater than or equal to zero and less than .
+ ///
+ /// The array to fill with random values.
+ public void NextInt32s(int[] values)
+ {
+ if (_threadSafe)
+ {
+ lock (_lock)
+ {
+ for (var i = 0; i < values.Length; i++)
+ {
+ values[i] = DoSampleInteger();
+ }
+ }
+ }
+ else
+ {
+ for (var i = 0; i < values.Length; i++)
+ {
+ values[i] = DoSampleInteger();
+ }
+ }
+ }
+
+ ///
+ /// Returns an array with random 32-bit signed integers greater than or equal to zero and less than .
+ ///
+ /// The size of the array to fill.
+ public int[] NextInt32s(int count)
+ {
+ var values = new int[count];
+ NextInt32s(values);
+ return values;
+ }
+
///
/// Fills an array with random numbers within a specified range.
///
@@ -194,6 +243,17 @@ namespace MathNet.Numerics.Random
/// The exclusive upper bound of the random number returned. must be greater than or equal to .
public void NextInt32s(int[] values, int minInclusive, int maxExclusive)
{
+ if (minInclusive > maxExclusive)
+ {
+ throw new ArgumentException(Resources.ArgumentMinValueGreaterThanMaxValue);
+ }
+
+ if (maxExclusive == int.MaxValue && minInclusive == 0)
+ {
+ NextInt32s(values);
+ return;
+ }
+
if (_threadSafe)
{
lock (_lock)
@@ -213,6 +273,40 @@ namespace MathNet.Numerics.Random
}
}
+ ///
+ /// Returns an array with random 32-bit signed integers within the specified range.
+ ///
+ /// The size of the array to fill.
+ /// 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 .
+ public int[] NextInt32s(int count, int minInclusive, int maxExclusive)
+ {
+ var values = new int[count];
+ NextInt32s(values, minInclusive, maxExclusive);
+ return values;
+ }
+
+ ///
+ /// Returns an infinite sequence of random 32-bit signed integers greater than or equal to zero and less than .
+ ///
+ public IEnumerable NextInt32Sequence()
+ {
+ for (int i = 0; i < 64; i++)
+ {
+ yield return Next();
+ }
+
+ var buffer = new int[64];
+ while (true)
+ {
+ NextInt32s(buffer);
+ for (int i = 0; i < buffer.Length; i++)
+ {
+ yield return buffer[i];
+ }
+ }
+ }
+
///
/// Returns an infinite sequence of random numbers within a specified range.
///
@@ -220,6 +314,11 @@ namespace MathNet.Numerics.Random
/// The exclusive upper bound of the random number returned. must be greater than or equal to .
public IEnumerable NextInt32Sequence(int minInclusive, int maxExclusive)
{
+ if (minInclusive > maxExclusive)
+ {
+ throw new ArgumentException(Resources.ArgumentMinValueGreaterThanMaxValue);
+ }
+
for (int i = 0; i < 64; i++)
{
yield return Next(minInclusive, maxExclusive);
@@ -290,7 +389,7 @@ namespace MathNet.Numerics.Random
protected abstract double DoSample();
///
- /// Returns a random 32-bit signed integer greater than or equal to zero and less than
+ /// Returns a random 32-bit signed integer greater than or equal to zero and less than .
///
protected virtual int DoSampleInteger()
{