@ -159,11 +159,11 @@ namespace MathNet.Numerics.Random
{
lock ( _l ock )
{
return DoSampleInteger ( 0 , maxExclusive ) ;
return DoSampleInteger ( maxExclusive ) ;
}
}
return DoSampleInteger ( 0 , maxExclusive ) ;
return DoSampleInteger ( maxExclusive ) ;
}
/// <summary>
@ -182,9 +182,14 @@ namespace MathNet.Numerics.Random
throw new ArgumentException ( Resources . ArgumentMinValueGreaterThanMaxValue ) ;
}
if ( maxExclusive = = int . MaxValue & & m inInclusive = = 0 )
if ( minInclusive = = 0 )
{
return Next ( ) ;
if ( maxExclusive = = int . MaxValue )
{
return Next ( ) ;
}
return Next ( maxExclusive ) ;
}
if ( _ threadSafe )
@ -234,6 +239,38 @@ namespace MathNet.Numerics.Random
return values ;
}
/// <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="maxExclusive">The exclusive upper bound of the random number returned.</param>
public void NextInt32s ( int [ ] values , int maxExclusive )
{
if ( maxExclusive = = int . MaxValue )
{
NextInt32s ( values ) ;
return ;
}
if ( _ threadSafe )
{
lock ( _l ock )
{
for ( var i = 0 ; i < values . Length ; i + + )
{
values [ i ] = DoSampleInteger ( maxExclusive ) ;
}
}
}
else
{
for ( var i = 0 ; i < values . Length ; i + + )
{
values [ i ] = DoSampleInteger ( maxExclusive ) ;
}
}
}
/// <summary>
/// Fills an array with random numbers within a specified range.
/// </summary>
@ -253,6 +290,18 @@ namespace MathNet.Numerics.Random
return ;
}
if ( minInclusive = = 0 )
{
if ( maxExclusive = = int . MaxValue )
{
NextInt32s ( values ) ;
return ;
}
NextInt32s ( values , maxExclusive ) ;
return ;
}
if ( _ threadSafe )
{
lock ( _l ock )
@ -389,16 +438,6 @@ namespace MathNet.Numerics.Random
return ( int ) ( DoSample ( ) * int . MaxValue ) ;
}
/// <summary>
/// Returns a random 32-bit signed integer within the specified range.
/// </summary>
/// <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>
protected virtual int DoSampleInteger ( int minInclusive , int maxExclusive )
{
return ( int ) ( DoSample ( ) * ( maxExclusive - minInclusive ) ) + minInclusive ;
}
/// <summary>
/// Fills the elements of a specified array of bytes with random numbers in full range, including zero and 255 (<see cref="F:System.Byte.MaxValue"/>).
/// </summary>
@ -409,5 +448,24 @@ namespace MathNet.Numerics.Random
buffer [ i ] = ( byte ) ( DoSampleInteger ( ) % 2 5 6 ) ;
}
}
/// <summary>
/// Returns a random 32-bit signed integer within the specified range.
/// </summary>
/// <param name="maxExclusive">The exclusive upper bound of the random number returned.</param>
protected virtual int DoSampleInteger ( int maxExclusive )
{
return ( int ) ( DoSample ( ) * maxExclusive ) ;
}
/// <summary>
/// Returns a random 32-bit signed integer within the specified range.
/// </summary>
/// <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>
protected virtual int DoSampleInteger ( int minInclusive , int maxExclusive )
{
return DoSampleInteger ( maxExclusive - minInclusive ) + minInclusive ;
}
}
}