Skip to content

Commit

Permalink
fixed unlisted bug in MersenneTwister.popFront
Browse files Browse the repository at this point in the history
  • Loading branch information
andralex committed Apr 22, 2009
1 parent c602c45 commit 8b87031
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions std/random.d
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ Parameter for the generator.
*/
void popFront()
{
if (mti == size_t.max) seed();
enum UIntType
upperMask = ~((cast(UIntType) 1u <<
(UIntType.sizeof * 8 - (w - r))) - 1),
Expand All @@ -355,20 +356,17 @@ Parameter for the generator.
if (mti >= n)
{
/* generate N words at one time */
if (mti == n + 1) /* if init_genrand() has not been called, */
{
seed(defaultSeed); /* a default initial seed is used */
return popFront;
}

int kk = 0;
for (; kk < n - m; ++kk)
const limit1 = n - m;
for (; kk < limit1; ++kk)
{
y = (mt[kk] & upperMask)|(mt[kk + 1] & lowerMask);
mt[kk] = cast(UIntType) (mt[kk + m] ^ (y >> 1)
^ mag01[cast(UIntType) y & 0x1U]);
^ mag01[cast(UIntType) y & 0x1U]);
}
for (; kk < n - 1; ++kk)
const limit2 = n - 1;
for (; kk < limit2; ++kk)
{
y = (mt[kk] & upperMask)|(mt[kk + 1] & lowerMask);
mt[kk] = cast(UIntType) (mt[kk + (m -n)] ^ (y >> 1)
Expand Down Expand Up @@ -397,10 +395,7 @@ Parameter for the generator.
*/
UIntType front()
{
if (mti == n + 1)
{
popFront;
}
if (mti == size_t.max) seed();
return _y;
}

Expand All @@ -410,7 +405,7 @@ Always $(D false).
enum bool empty = false;

private UIntType mt[n];
private size_t mti = n + 1; /* means mt is not initialized */
private size_t mti = size_t.max; /* means mt is not initialized */
UIntType _y = UIntType.max;
}

Expand Down Expand Up @@ -444,6 +439,22 @@ unittest
assert(gen.front == 4123659995);
}

unittest
{
uint a, b;
{
Mt19937 gen;
a = gen.front;
}
{
Mt19937 gen;
gen.popFront;
//advance(gen, 1); // skip 1 element
b = gen.front;
}
assert(a != b);
}

/**
A "good" seed for initializing random number engines. Initializing
with $(D_PARAM unpredictableSeed) makes engines generate different
Expand Down Expand Up @@ -767,7 +778,7 @@ struct RandomCover(Range, Random)
{
if (_chosen[i]) { ++i; continue; }
// Roll a dice with k faces
auto chooseMe = uniform(0, k - 1, _rnd) == 0;
auto chooseMe = uniform(0, k, _rnd) == 0;
assert(k > 1 || chooseMe);
if (chooseMe)
{
Expand Down

0 comments on commit 8b87031

Please sign in to comment.