Browse Source

Added extension methods for System.Random that sample additional value types.

Signed-off-by: jvangael <jurgen.vangael@gmail.com>
la-knuth
Jurgen Van Gael 17 years ago
parent
commit
7592ccb50b
  1. 1
      src/Numerics/Numerics.csproj
  2. 117
      src/Numerics/Random/SystemRandomExtensions.cs
  3. 65
      src/UnitTests/Random/SystemRandomExtensionTests.cs
  4. 1
      src/UnitTests/UnitTests.csproj

1
src/Numerics/Numerics.csproj

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

117
src/Numerics/Random/SystemRandomExtensions.cs

@ -0,0 +1,117 @@
// <copyright file="SystemRandomExtensions.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;
public static class SystemRandomExtensions
{
/// <summary>
/// Returns a nonnegative random number less than <see cref="Int64.MaxValue"/>.
/// </summary>
/// <returns>
/// A 64-bit signed integer greater than or equal to 0, and less than <see cref="Int64.MaxValue"/>; that is,
/// the range of return values includes 0 but not <paramref name="Int64.MaxValue"/>.
/// </returns>
/// <seealso cref="NextFullRangeInt64()"/>
public static long NextInt64(this System.Random rnd)
{
byte[] buffer = new byte[sizeof(Int64)];
rnd.NextBytes(buffer);
long candidate = BitConverter.ToInt64(buffer, 0);
// wrap negative numbers around, mapping every negative number to a distinct nonnegative number
// MinValue -> 0, -1 -> MaxValue
candidate &= Int64.MaxValue;
// skip candidate if it is MaxValue. Recursive since rare.
return (candidate == Int64.MaxValue) ? rnd.NextInt64() : candidate;
}
/// <summary>
/// Returns a random number of the full Int32 range.
/// </summary>
/// <returns>
/// A 32-bit signed integer of the full range, including 0, negative numbers,
/// <see cref="Int32.MaxValue"/> and <see cref="Int32.MinValue"/>.
/// </returns>
/// <seealso cref="Next()"/>
public static int NextFullRangeInt32(this System.Random rnd)
{
byte[] buffer = new byte[sizeof(Int32)];
rnd.NextBytes(buffer);
return BitConverter.ToInt32(buffer, 0);
}
/// <summary>
/// Returns a random number of the full Int64 range.
/// </summary>
/// <returns>
/// A 64-bit signed integer of the full range, including 0, negative numbers,
/// <see cref="Int64.MaxValue"/> and <see cref="Int64.MinValue"/>.
/// </returns>
/// <seealso cref="NextInt64()"/>
public static long NextFullRangeInt64(this System.Random rnd)
{
byte[] buffer = new byte[sizeof(Int64)];
rnd.NextBytes(buffer);
return BitConverter.ToInt64(buffer, 0);
}
/// <summary>
/// Returns a nonnegative decimal floating point random number less than 1.0.
/// </summary>
/// <returns>
/// A decimal floating point number greater than or equal to 0.0, and less than 1.0; that is,
/// the range of return values includes 0.0 but not 1.0.
/// </returns>
public static decimal NextDecimal(this System.Random rnd)
{
decimal candidate;
// 50.049 % chance that the number is below 1.0. Try until we have one.
// Guarantees that any decimal in the interval can
// indeed be reached, with uniform probability.
do
{
candidate = new Decimal(
rnd.NextFullRangeInt32(),
rnd.NextFullRangeInt32(),
rnd.NextFullRangeInt32(),
false,
28);
}
while (candidate >= 1.0m);
return candidate;
}
}
}

65
src/UnitTests/Random/SystemRandomExtensionTests.cs

@ -0,0 +1,65 @@
// <copyright file="SystemRandomExtensionTests.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.UnitTests.DistributionTests
{
using MbUnit.Framework;
using MathNet.Numerics.Random;
[TestFixture]
public class SystemRandomExtensionTests
{
[Test]
public void CanSampleInt64()
{
var rnd = new System.Random();
long l = rnd.NextInt64();
}
[Test]
public void CanSampleFullRangeInt32()
{
var rnd = new System.Random();
int l = rnd.NextFullRangeInt32();
}
[Test]
public void CanSampleFullRangeInt64()
{
var rnd = new System.Random();
long l = rnd.NextFullRangeInt64();
}
[Test]
public void CanSampleDecimal()
{
var rnd = new System.Random();
decimal l = rnd.NextDecimal();
}
}
}

1
src/UnitTests/UnitTests.csproj

@ -92,6 +92,7 @@
<Compile Include="NumberTheoryTests\IntegerTheoryTest.cs" />
<Compile Include="PrecisionTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Random\SystemRandomExtensionTests.cs" />
<Compile Include="SortingTests.cs" />
<Compile Include="SpecialFunctionsTests\ErfTests.cs" />
<Compile Include="SpecialFunctionsTests\FactorialTest.cs" />

Loading…
Cancel
Save