|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
namespace RandomOps
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Ran2 : RanInt32
|
|
|
{
|
|
|
#region Constructors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Ran2()
|
|
|
: base()
|
|
|
{
|
|
|
Seed();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Ran2(Int32 seed)
|
|
|
: base()
|
|
|
{
|
|
|
Seed(seed);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Iterator-class (internal use only).
|
|
|
class Iterator
|
|
|
{
|
|
|
Int32 IM, IA, IQ, IR;
|
|
|
|
|
|
public Int32 Idum
|
|
|
{
|
|
|
get;
|
|
|
private set;
|
|
|
}
|
|
|
|
|
|
public Iterator(Int32 im, Int32 ia, Int32 iq, Int32 ir)
|
|
|
{
|
|
|
IM = im;
|
|
|
IA = ia;
|
|
|
IQ = iq;
|
|
|
IR = ir;
|
|
|
}
|
|
|
|
|
|
public void Seed(Int32 seed)
|
|
|
{
|
|
|
Idum = seed;
|
|
|
}
|
|
|
|
|
|
public Int32 DoRand()
|
|
|
{
|
|
|
Int32 k;
|
|
|
|
|
|
k = Idum / IQ;
|
|
|
|
|
|
Idum = IA * (Idum - k * IQ) - IR * k;
|
|
|
|
|
|
if (Idum < 0)
|
|
|
{
|
|
|
Idum += IM;
|
|
|
}
|
|
|
|
|
|
return Idum;
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Internal definitions and variables.
|
|
|
static readonly Int32 IM0 = 2147483563;
|
|
|
static readonly Int32 IM1 = 2147483399;
|
|
|
static readonly Int32 IA0 = 40014;
|
|
|
static readonly Int32 IA1 = 40692;
|
|
|
static readonly Int32 IQ0 = 53668;
|
|
|
static readonly Int32 IQ1 = 52774;
|
|
|
static readonly Int32 IR0 = 12211;
|
|
|
static readonly Int32 IR1 = 3791;
|
|
|
static readonly Int32 NTAB = 32;
|
|
|
static readonly Int32 IMM = IM0 - 1;
|
|
|
static readonly Int32 NDIV = 1 + IMM / NTAB;
|
|
|
static readonly Int32 WARMUP = 1024 + 8;
|
|
|
static readonly Int32 WARMUP2 = 200;
|
|
|
|
|
|
Iterator[] Iterators = { new Iterator(IM0, IA0, IQ0, IR0), new Iterator(IM1, IA1, IQ1, IR1) };
|
|
|
|
|
|
Int32 iy = 0;
|
|
|
Int32[] iv = new Int32[NTAB];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool IsReady = false;
|
|
|
#endregion
|
|
|
|
|
|
#region PRNG Implementation.
|
|
|
|
|
|
|
|
|
|
|
|
public sealed override Int32 Rand()
|
|
|
{
|
|
|
Debug.Assert(IsReady);
|
|
|
|
|
|
Iterators[0].DoRand();
|
|
|
Iterators[1].DoRand();
|
|
|
|
|
|
{
|
|
|
|
|
|
int j = iy / NDIV;
|
|
|
|
|
|
Debug.Assert(j >= 0 && j < NTAB);
|
|
|
|
|
|
|
|
|
|
|
|
iy = iv[j] - Iterators[1].Idum;
|
|
|
iv[j] = Iterators[0].Idum;
|
|
|
|
|
|
if (iy < 1)
|
|
|
{
|
|
|
iy += IMM;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Debug.Assert(iy >= 0 && iy <= RandMax);
|
|
|
|
|
|
return iy;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public sealed override Int32 RandMax
|
|
|
{
|
|
|
get { return IM0 - 1; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected sealed override void Seed(Int32 seed)
|
|
|
{
|
|
|
int j;
|
|
|
|
|
|
|
|
|
if (seed == 0)
|
|
|
{
|
|
|
seed = 1;
|
|
|
}
|
|
|
else if (seed < 0)
|
|
|
{
|
|
|
seed = -seed;
|
|
|
}
|
|
|
|
|
|
Iterators[0].Seed(seed);
|
|
|
Iterators[1].Seed(seed);
|
|
|
|
|
|
|
|
|
for (j = 0; j < WARMUP; j++)
|
|
|
{
|
|
|
Iterators[0].DoRand();
|
|
|
}
|
|
|
|
|
|
for (j = NTAB - 1; j >= 0; j--)
|
|
|
{
|
|
|
iv[j] = Iterators[0].DoRand();
|
|
|
}
|
|
|
|
|
|
iy = iv[0];
|
|
|
|
|
|
|
|
|
IsReady = true;
|
|
|
|
|
|
|
|
|
for (j = 0; j < WARMUP2; j++)
|
|
|
{
|
|
|
Rand();
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Base-class overrides.
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
{
|
|
|
get { return "Ran2"; }
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
}
|
|
|
|