/// ------------------------------------------------------
/// RandomOps - (Pseudo) Random Number Generator For C#
/// Copyright (C) 2003-2010 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// RandomOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------
namespace RandomOps
{
///
/// Wrapper for the .NET built-in PRNG. Not thread-safe by
/// default.
///
///
/// Since the .NET implementation may change we cannot
/// override and make a more efficient implementation of
/// Byte() that would use bit-manipulation, because it
/// would assume things about the implementation that might
/// change.
///
public class RanSystem : Random
{
#region Constructors.
///
/// Constructs the PRNG-object and seeds the PRNG with the current time of day.
/// This is what you will mostly want to use.
///
public RanSystem()
: base()
{
Rand = new System.Random();
}
///
/// Constructs the PRNG-object using the designated seed.
/// This is useful if you want to repeat experiments with the
/// same sequence of pseudo-random numbers.
///
public RanSystem(int seed)
: base()
{
Rand = new System.Random(seed);
}
#endregion
#region Internal variables.
///
/// The .NET PRNG-object.
///
System.Random Rand;
#endregion
#region Base-class overrides.
///
/// Name of the RNG.
///
public override string Name
{
get { return "RanSystem"; }
}
///
/// Draw a uniform random number in the exclusive range (0,1)
///
///
public sealed override double Uniform()
{
return Rand.NextDouble();
}
///
/// Draw a random boolean with equal probability of drawing true or false.
///
///
public sealed override bool Bool()
{
return Rand.Next(2) == 0;
}
///
/// Draw a random index from the range {0, .., n-1}
///
/// The exclusive upper bound.
///
public sealed override int Index(int n)
{
return Rand.Next(n);
}
///
/// Draw a random and uniform byte.
///
public sealed override byte Byte()
{
// Re-use the Index() method.
return Bytes(1)[0];
}
///
/// Draw an array of random and uniform bytes.
///
/// The array length requested.
public sealed override byte[] Bytes(int length)
{
byte[] arr = new byte[length];
Rand.NextBytes(arr);
return arr;
}
#endregion
}
}