/// ------------------------------------------------------
/// 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/
/// ------------------------------------------------------
using System;
using System.Diagnostics;
namespace RandomOps
{
///
/// Pseudo-Random Number Generator (PRNG) base-class for a generator of UInt32 integers.
///
///
/// It is somewhat tricky to implement Index() using integer-operations and get the
/// rounding right for all cases, so we reuse the base-class implementation which
/// indirectly uses Uniform().
///
public abstract class RanInt32 : Random
{
#region Constructors.
///
/// Constructs the PRNG-object.
///
public RanInt32()
: base()
{
_randMaxHalf = RandMax / 2;
_randInv = 1.0/((double)RandMax + 2);
}
#endregion
#region Internal variables.
///
/// Used in Bool(), for convenience and speed.
///
Int32 _randMaxHalf;
///
/// Used in Uniform(), for convenience and speed.
///
double _randInv;
#endregion
#region PRNG Implementation.
///
/// Draw a random number in inclusive range {0, .., RandMax}
///
public abstract Int32 Rand();
///
/// The maximum possible value returned by Rand().
///
public abstract Int32 RandMax
{
get;
}
///
/// Seed with the time of day.
///
protected void Seed()
{
Int32 seed = (Int32)(DateTime.Now.Ticks % (long)RandMax);
Seed(seed);
}
///
/// Seed with an integer.
///
protected abstract void Seed(Int32 seed);
#endregion
#region Base-class overrides.
///
/// Draw a uniform random number in the exclusive range (0,1)
/// Thread-safe if Rand() is thread-safe.
///
///
/// Assumes that Rand() is in {0, .., RandMax}.
///
public override double Uniform()
{
double rand = (double)Rand() + 1;
double value = rand * _randInv;
Debug.Assert(value > 0 && value < 1);
return value;
}
///
/// Draw a random boolean with equal probability of true or false.
/// Thread-safe if Rand() is thread-safe.
///
public override bool Bool()
{
return Rand() < _randMaxHalf;
}
///
/// Draw a random and uniform byte.
/// Thread-safe if Rand() is thread-safe.
///
///
/// The least significant bits are not that statistically random,
/// hence we must use the most significant bits by a bit-shift.
///
public override byte Byte()
{
Int32 r = Rand();
Int32 value = r >> 23;
Debug.Assert(value >= 0 && value <= System.Byte.MaxValue);
byte b = (byte)value;
return b;
}
#endregion
}
}