From 9bb432aef8d8b5d20137eb8470a767a7669b4171 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 24 Jun 2014 10:16:39 +0200 Subject: [PATCH] Random: generate a sequence of integers within a range in one go --- src/Numerics/Random/RandomExtensions.cs | 52 +++++++++++++++++++++++++ src/Numerics/Random/RandomSource.cs | 50 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/Numerics/Random/RandomExtensions.cs b/src/Numerics/Random/RandomExtensions.cs index 50aa5e73..2a120035 100644 --- a/src/Numerics/Random/RandomExtensions.cs +++ b/src/Numerics/Random/RandomExtensions.cs @@ -121,6 +121,58 @@ namespace MathNet.Numerics.Random return values; } + /// + /// Fills an array with uniform random numbers greater than or equal to 0.0 and less than 1.0. + /// + /// The random number generator. + /// The array to fill with random values. + /// Lower bound, inclusive. + /// Upper bound, exclusive. + /// + /// 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, 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); + } + } + + /// + /// Returns an infinite sequence of uniform random numbers greater than or equal to 0.0 and less than 1.0. + /// + /// + /// 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 IEnumerable 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 NextInt32SequenceEnumerable(System.Random rnd, int minInclusive, int maxExclusive) + { + while (true) + { + yield return rnd.Next(minInclusive, maxExclusive); + } + } + /// /// Returns a nonnegative random number less than . /// diff --git a/src/Numerics/Random/RandomSource.cs b/src/Numerics/Random/RandomSource.cs index d0af5ee1..8b19933e 100644 --- a/src/Numerics/Random/RandomSource.cs +++ b/src/Numerics/Random/RandomSource.cs @@ -181,6 +181,56 @@ namespace MathNet.Numerics.Random return (int)(DoSample()*(maxValue - minValue)) + minValue; } + /// + /// Fills an array with random numbers within a specified range. + /// + /// The array to fill with random values. + /// 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 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; + } + } + } + + /// + /// Returns an infinite sequence of random numbers within a 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 . + public IEnumerable 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]; + } + } + } + /// /// Fills the elements of a specified array of bytes with random numbers. ///