Browse Source

Random: generate a sequence of integers within a range in one go

pull/225/merge
Christoph Ruegg 12 years ago
parent
commit
9bb432aef8
  1. 52
      src/Numerics/Random/RandomExtensions.cs
  2. 50
      src/Numerics/Random/RandomSource.cs

52
src/Numerics/Random/RandomExtensions.cs

@ -121,6 +121,58 @@ namespace MathNet.Numerics.Random
return values;
}
/// <summary>
/// Fills an array with uniform random numbers greater than or equal to 0.0 and less than 1.0.
/// </summary>
/// <param name="rnd">The random number generator.</param>
/// <param name="values">The array to fill with random values.</param>
/// <param name="minInclusive">Lower bound, inclusive.</param>
/// <param name="maxExclusive">Upper bound, exclusive.</param>
/// <remarks>
/// 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.
/// </remarks>
public static void NextInt32s(this System.Random rnd, int[] values, int minInclusive, int maxExclusive)
{
var rs = rnd as RandomSource;
if (rs != null)
{
rs.NextInt32s(values, minInclusive, maxExclusive);
return;
}
for (var i = 0; i < values.Length; i++)
{
values[i] = rnd.Next(minInclusive, maxExclusive);
}
}
/// <summary>
/// Returns an infinite sequence of uniform random numbers greater than or equal to 0.0 and less than 1.0.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public static IEnumerable<int> NextInt32Sequence(this System.Random rnd, int minInclusive, int maxExclusive)
{
var rs = rnd as RandomSource;
if (rs != null)
{
return rs.NextInt32Sequence(minInclusive, maxExclusive);
}
return NextInt32SequenceEnumerable(rnd, minInclusive, maxExclusive);
}
static IEnumerable<int> NextInt32SequenceEnumerable(System.Random rnd, int minInclusive, int maxExclusive)
{
while (true)
{
yield return rnd.Next(minInclusive, maxExclusive);
}
}
/// <summary>
/// Returns a nonnegative random number less than <see cref="Int64.MaxValue"/>.
/// </summary>

50
src/Numerics/Random/RandomSource.cs

@ -181,6 +181,56 @@ namespace MathNet.Numerics.Random
return (int)(DoSample()*(maxValue - minValue)) + minValue;
}
/// <summary>
/// Fills an array with random numbers within a specified range.
/// </summary>
/// <param name="values">The array to fill with random values.</param>
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned. <paramref name="maxValue"/> must be greater than or equal to <paramref name="minValue"/>.</param>
public void NextInt32s(int[] values, int minValue, int maxValue)
{
if (_threadSafe)
{
lock (_lock)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = (int)(DoSample()*(maxValue - minValue)) + minValue;
}
}
}
else
{
for (var i = 0; i < values.Length; i++)
{
values[i] = (int)(DoSample()*(maxValue - minValue)) + minValue;
}
}
}
/// <summary>
/// Returns an infinite sequence of random numbers within a specified range.
/// </summary>
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned. <paramref name="maxValue"/> must be greater than or equal to <paramref name="minValue"/>.</param>
public IEnumerable<int> NextInt32Sequence(int minValue, int maxValue)
{
for (int i = 0; i < 64; i++)
{
yield return Next(minValue, maxValue);
}
var buffer = new int[64];
while (true)
{
NextInt32s(buffer, minValue, maxValue);
for (int i = 0; i < buffer.Length; i++)
{
yield return buffer[i];
}
}
}
/// <summary>
/// Fills the elements of a specified array of bytes with random numbers.
/// </summary>

Loading…
Cancel
Save