@ -90,6 +90,17 @@ namespace MathNet.Numerics.Random
}
}
/// <summary>
/// Returns an array of uniform random numbers greater than or equal to 0.0 and less than 1.0.
/// </summary>
/// <param name="count">The size of the array to fill.</param>
public double [ ] NextDoubles ( int count )
{
var values = new double [ count ] ;
NextDoubles ( values ) ;
return values ;
}
/// <summary>
/// Returns an infinite sequence of uniform random numbers greater than or equal to 0.0 and less than 1.0.
/// </summary>
@ -112,11 +123,8 @@ namespace MathNet.Numerics.Random
}
/// <summary>
/// Returns a nonnegative random number .
/// Returns a random 32-bit signed integer greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/> .
/// </summary>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>.
/// </returns>
public sealed override int Next ( )
{
if ( _ threadSafe )
@ -133,17 +141,17 @@ namespace MathNet.Numerics.Random
/// <summary>
/// Returns a random number less then a specified maximum.
/// </summary>
/// <param name="maxValu e">The exclusive upper bound of the random number returned.</param>
/// <returns>A 32-bit signed integer less than <paramref name="maxValu e"/>.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="maxValu e"/> is negative. </exception>
public sealed override int Next ( int maxValu e )
/// <param name="maxExclusiv e">The exclusive upper bound of the random number returned.</param>
/// <returns>A 32-bit signed integer less than <paramref name="maxExclusiv e"/>.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="maxExclusiv e"/> is negative. </exception>
public sealed override int Next ( int maxExclusiv e )
{
if ( maxValu e < = 0 )
if ( maxExclusiv e < = 0 )
{
throw new ArgumentException ( Resources . ArgumentMustBePositive ) ;
}
if ( maxValu e = = int . MaxValue )
if ( maxExclusiv e = = int . MaxValue )
{
return Next ( ) ;
}
@ -152,11 +160,11 @@ namespace MathNet.Numerics.Random
{
lock ( _l ock )
{
return DoSampleInteger ( 0 , maxValu e ) ;
return DoSampleInteger ( 0 , maxExclusiv e ) ;
}
}
return DoSampleInteger ( 0 , maxValu e ) ;
return DoSampleInteger ( 0 , maxExclusiv e ) ;
}
/// <summary>
@ -175,6 +183,11 @@ namespace MathNet.Numerics.Random
throw new ArgumentException ( Resources . ArgumentMinValueGreaterThanMaxValue ) ;
}
if ( maxExclusive = = int . MaxValue & & minInclusive = = 0 )
{
return Next ( ) ;
}
if ( _ threadSafe )
{
lock ( _l ock )
@ -186,6 +199,42 @@ namespace MathNet.Numerics.Random
return DoSampleInteger ( minInclusive , maxExclusive ) ;
}
/// <summary>
/// Fills an array with random 32-bit signed integers greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>.
/// </summary>
/// <param name="values">The array to fill with random values.</param>
public void NextInt32s ( int [ ] values )
{
if ( _ threadSafe )
{
lock ( _l ock )
{
for ( var i = 0 ; i < values . Length ; i + + )
{
values [ i ] = DoSampleInteger ( ) ;
}
}
}
else
{
for ( var i = 0 ; i < values . Length ; i + + )
{
values [ i ] = DoSampleInteger ( ) ;
}
}
}
/// <summary>
/// Returns an array with random 32-bit signed integers greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>.
/// </summary>
/// <param name="count">The size of the array to fill.</param>
public int [ ] NextInt32s ( int count )
{
var values = new int [ count ] ;
NextInt32s ( values ) ;
return values ;
}
/// <summary>
/// Fills an array with random numbers within a specified range.
/// </summary>
@ -194,6 +243,17 @@ namespace MathNet.Numerics.Random
/// <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>
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 ( _l ock )
@ -213,6 +273,40 @@ namespace MathNet.Numerics.Random
}
}
/// <summary>
/// Returns an array with random 32-bit signed integers within the specified range.
/// </summary>
/// <param name="count">The size of the array to fill.</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>
public int [ ] NextInt32s ( int count , int minInclusive , int maxExclusive )
{
var values = new int [ count ] ;
NextInt32s ( values , minInclusive , maxExclusive ) ;
return values ;
}
/// <summary>
/// Returns an infinite sequence of random 32-bit signed integers greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>.
/// </summary>
public IEnumerable < int > NextInt32Sequence ( )
{
for ( int i = 0 ; i < 6 4 ; i + + )
{
yield return Next ( ) ;
}
var buffer = new int [ 6 4 ] ;
while ( true )
{
NextInt32s ( buffer ) ;
for ( int i = 0 ; i < buffer . Length ; i + + )
{
yield return buffer [ i ] ;
}
}
}
/// <summary>
/// Returns an infinite sequence of random numbers within a specified range.
/// </summary>
@ -220,6 +314,11 @@ namespace MathNet.Numerics.Random
/// <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>
public IEnumerable < int > NextInt32Sequence ( int minInclusive , int maxExclusive )
{
if ( minInclusive > maxExclusive )
{
throw new ArgumentException ( Resources . ArgumentMinValueGreaterThanMaxValue ) ;
}
for ( int i = 0 ; i < 6 4 ; i + + )
{
yield return Next ( minInclusive , maxExclusive ) ;
@ -290,7 +389,7 @@ namespace MathNet.Numerics.Random
protected abstract double DoSample ( ) ;
/// <summary>
/// Returns a random 32-bit signed integer greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>
/// Returns a random 32-bit signed integer greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>.
/// </summary>
protected virtual int DoSampleInteger ( )
{