Browse Source

added AbstractRandomNumberGenerator.cs

Signed-off-by: jvangael <jurgen.vangael@gmail.com>
la-knuth
Jurgen Van Gael 17 years ago
parent
commit
ab86ca23c6
  1. 12
      src/FSharpUnitTests/Program.fs
  2. 1
      src/Numerics/Numerics.csproj
  3. 11
      src/Numerics/Properties/Resources.Designer.cs
  4. 3
      src/Numerics/Properties/Resources.resx
  5. 194
      src/Numerics/Random/AbstractRandomNumberGenerator.cs

12
src/FSharpUnitTests/Program.fs

@ -13,17 +13,15 @@ let DenseVectorTests =
specs "DenseVector" [
spec "DenseVector.init"
(Double.DenseVector.init 100 (fun i -> float i / 100.0) |> should approximately_vector_equal 16 largev)
(Double.DenseVector.init 100 (fun i -> float i / 100.0) |> should equal largev)
spec "DenseVector.of_list"
(Double.DenseVector.of_list [ for i in 0 .. 99 -> float i / 100.0 ] |> should approximately_vector_equal 16 largev)
(Double.DenseVector.of_list [ for i in 0 .. 99 -> float i / 100.0 ] |> should equal largev)
spec "DenseVector.of_seq"
(Double.DenseVector.of_seq (seq { for i in 0 .. 99 -> float i / 100.0 }) |> should approximately_vector_equal 16 largev)
(Double.DenseVector.of_seq (seq { for i in 0 .. 99 -> float i / 100.0 }) |> should equal largev)
spec "DenseVector.rangef"
(Double.DenseVector.rangef 0.0 0.01 0.99
|> should approximately_vector_equal 16 (new Double.DenseVector( [| for i in 0 .. 99 -> 0.01 * float i |] ) ))
(Double.DenseVector.rangef 0.0 0.01 0.99 |> should equal (new Double.DenseVector( [| for i in 0 .. 99 -> 0.01 * float i |] ) ))
spec "DenseVector.range"
(Double.DenseVector.range 0 99
|> should approximately_vector_equal 16 (new Double.DenseVector( [| for i in 0 .. 99 -> float i |] ) ))
(Double.DenseVector.range 0 99 |> should equal (new Double.DenseVector( [| for i in 0 .. 99 -> float i |] ) ))
]
/// Report on errors and success and exit.

1
src/Numerics/Numerics.csproj

@ -100,6 +100,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Random\AbstractRandomNumberGenerator.cs" />
<Compile Include="Random\SystemRandomExtensions.cs" />
<Compile Include="Sampling\Sample.Random.cs" />
<Compile Include="Sampling\Sample.Chebyshev.cs" />

11
src/Numerics/Properties/Resources.Designer.cs

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4927
// Runtime Version:2.0.50727.3074
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@ -222,6 +222,15 @@ namespace MathNet.Numerics.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to In the specified range, the minimum is greater than maximum..
/// </summary>
internal static string ArgumentMinValueGreaterThanMaxValue {
get {
return ResourceManager.GetString("ArgumentMinValueGreaterThanMaxValue", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Value must be positive..
/// </summary>

3
src/Numerics/Properties/Resources.resx

@ -264,4 +264,7 @@
<data name="UserDefinedProviderNotSpecified" xml:space="preserve">
<value>A user defined provider has not been specified.</value>
</data>
<data name="ArgumentMinValueGreaterThanMaxValue" xml:space="preserve">
<value>In the specified range, the minimum is greater than maximum.</value>
</data>
</root>

194
src/Numerics/Random/AbstractRandomNumberGenerator.cs

@ -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…
Cancel
Save