forked from tsai/mathnet-numerics
5 changed files with 213 additions and 8 deletions
@ -0,0 +1,194 @@ |
|||||
|
// <copyright file="AbstractRandomNumberGenerator.cs" company="Math.NET">
|
||||
|
// Math.NET Numerics, part of the Math.NET Project
|
||||
|
// http://mathnet.opensourcedotnet.info
|
||||
|
//
|
||||
|
// Copyright (c) 2009 Math.NET
|
||||
|
//
|
||||
|
// Permission is hereby granted, free of charge, to any person
|
||||
|
// obtaining a copy of this software and associated documentation
|
||||
|
// files (the "Software"), to deal in the Software without
|
||||
|
// restriction, including without limitation the rights to use,
|
||||
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
|
// copies of the Software, and to permit persons to whom the
|
||||
|
// Software is furnished to do so, subject to the following
|
||||
|
// conditions:
|
||||
|
//
|
||||
|
// The above copyright notice and this permission notice shall be
|
||||
|
// included in all copies or substantial portions of the Software.
|
||||
|
//
|
||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace MathNet.Numerics.Random |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Properties; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Abstract class for random number generators. This class introduces a layer between <see cref="System.Random"/>
|
||||
|
/// and the Math.Net Numerics random number generators to provide thread safety.
|
||||
|
/// </summary>
|
||||
|
public abstract class AbstractRandomNumberGenerator : System.Random |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// A delegate type that represents a method that generates random numbers.
|
||||
|
/// </summary>
|
||||
|
/// <returns>Randomly distributed numbers.</returns>
|
||||
|
private delegate double SampleMethod(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The method that actually generates samples.
|
||||
|
/// </summary>
|
||||
|
private readonly SampleMethod _sampleMethod; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The object that will be locked for thread safety.
|
||||
|
/// </summary>
|
||||
|
private readonly object _lock = new object(); |
||||
|
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="AbstractRandomNumberGenerator"/> class using
|
||||
|
/// the value of <see cref="Control.ThreadSafeRandomNumberGenerators"/> to set whether
|
||||
|
/// the instance is thread safe or not.
|
||||
|
/// </summary>
|
||||
|
protected AbstractRandomNumberGenerator(): this(Control.ThreadSafeRandomNumberGenerators) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="AbstractRandomNumberGenerator"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="threadSafe">if set to <c>true</c> , the class is thread safe.</param>
|
||||
|
/// <remarks>Thread safe instances are two and half times slower than non-thread
|
||||
|
/// safe classes.</remarks>
|
||||
|
protected AbstractRandomNumberGenerator(bool threadSafe) |
||||
|
{ |
||||
|
_sampleMethod = threadSafe ? (SampleMethod) ThreadSafeSample : DoSample; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns an array of uniformly distributed random doubles in the interval [0.0,1.0].
|
||||
|
/// </summary>
|
||||
|
/// <param name="n">The size of the array.</param>
|
||||
|
/// <returns>
|
||||
|
/// An array of uniformly distributed random doubles in the interval [0.0,1.0].
|
||||
|
/// </returns>
|
||||
|
/// <exception cref="ArgumentException">if n is not greater than 0.</exception>
|
||||
|
public double[] NextDouble(int n) |
||||
|
{ |
||||
|
if (n < 1) |
||||
|
{ |
||||
|
throw new ArgumentException(Resources.ArgumentMustBePositive); |
||||
|
} |
||||
|
var ret = new double[n]; |
||||
|
for (int i = 0; i < ret.Length; i++) |
||||
|
{ |
||||
|
ret[i] = Sample(); |
||||
|
} |
||||
|
return ret; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns a nonnegative random number.
|
||||
|
/// </summary>
|
||||
|
/// <returns>
|
||||
|
/// A 32-bit signed integer greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>.
|
||||
|
/// </returns>
|
||||
|
public override int Next() |
||||
|
{ |
||||
|
return (int)(Sample() * int.MaxValue); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns a random number less then a specified maximum.
|
||||
|
/// </summary>
|
||||
|
/// <param name="maxValue">The exclusive upper bound of the random number returned.</param>
|
||||
|
/// <returns>A 32-bit signed integer less than <paramref name="maxValue"/>.</returns>
|
||||
|
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="maxValue"/> is negative. </exception>
|
||||
|
public override int Next(int maxValue) |
||||
|
{ |
||||
|
if (0 > maxValue) |
||||
|
{ |
||||
|
throw new ArgumentOutOfRangeException(Resources.ArgumentMustBePositive); |
||||
|
} |
||||
|
|
||||
|
return (int) (Sample() % maxValue); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns a random number 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>
|
||||
|
/// <returns>
|
||||
|
/// A 32-bit signed integer greater than or equal to <paramref name="minValue"/> and less than <paramref name="maxValue"/>; that is, the range of return values includes <paramref name="minValue"/> but not <paramref name="maxValue"/>. If <paramref name="minValue"/> equals <paramref name="maxValue"/>, <paramref name="minValue"/> is returned.
|
||||
|
/// </returns>
|
||||
|
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="minValue"/> is greater than <paramref name="maxValue"/>. </exception>
|
||||
|
public override int Next(int minValue, int maxValue) |
||||
|
{ |
||||
|
if (minValue > maxValue) |
||||
|
{ |
||||
|
throw new ArgumentOutOfRangeException(Resources.ArgumentMinValueGreaterThanMaxValue); |
||||
|
} |
||||
|
|
||||
|
return (int) (Sample()*(maxValue - minValue)) + minValue; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Fills the elements of a specified array of bytes with random numbers.
|
||||
|
/// </summary>
|
||||
|
/// <param name="buffer">An array of bytes to contain random numbers.</param>
|
||||
|
/// <exception cref="T:System.ArgumentNullException"><paramref name="buffer"/> is null. </exception>
|
||||
|
public override void NextBytes(byte[] buffer) |
||||
|
{ |
||||
|
if (buffer == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException("buffer"); |
||||
|
} |
||||
|
|
||||
|
for (int i = 0; i < buffer.Length; i++) |
||||
|
{ |
||||
|
buffer[i] = (byte) (Next()%256); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns a random number between 0.0 and 1.0.
|
||||
|
/// </summary>
|
||||
|
/// <returns>A double-precision floating point number greater than or equal to 0.0, and less than 1.0.</returns>
|
||||
|
protected override double Sample() |
||||
|
{ |
||||
|
return _sampleMethod(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Thread safe version of <seealso cref="DoSample"/> which returns a random number between 0.0 and 1.0.
|
||||
|
/// </summary>
|
||||
|
/// <returns></returns>
|
||||
|
private double ThreadSafeSample() |
||||
|
{ |
||||
|
lock(_lock) |
||||
|
{ |
||||
|
return DoSample(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns a random number between 0.0 and 1.0.
|
||||
|
/// </summary>
|
||||
|
/// <returns>
|
||||
|
/// A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
|
||||
|
/// </returns>
|
||||
|
protected abstract double DoSample(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue