I run unittest a lot and from time to time I see this error:
Error: AssertError Failure std.random(476)
It's hard to reproduce.
Comment #1 by andrei — 2008-09-10T22:28:21Z
The unpredictable seed is meant to vary from one run to the next, not necessarily from one call to the next, so the test is a bit incorrect. The function does this:
uint unpredictableSeed()
{
static uint moseghint = 87324921;
return cast(uint) (getpid ^ getUTCtime ^ ++moseghint);
}
(Bonus q: what does moseghint mean?)
If it so happens that getUTCtime and moseghint e.g. change only LSB in lockstep from one call to the next, the returned values will be identical. I changed the function to this:
uint unpredictableSeed()
{
static bool seeded;
static MinstdRand0 rand;
if (!seeded) {
rand.seed(getpid ^ getUTCtime);
}
return cast(uint) (getUTCtime ^ rand.next);
}
It'll be a bit slower but also quite a bit less predictable. But I also removed the assert because even with the implementation above, it's not guaranteed that two successive returns can't be equal.
Comment #2 by andrei — 2008-09-10T22:47:41Z
(In reply to comment #1)
> The unpredictable seed is meant to vary from one run to the next, not
> necessarily from one call to the next, so the test is a bit incorrect. The
> function does this:
>
> uint unpredictableSeed()
> {
> static uint moseghint = 87324921;
> return cast(uint) (getpid ^ getUTCtime ^ ++moseghint);
> }
>
> (Bonus q: what does moseghint mean?)
>
> If it so happens that getUTCtime and moseghint e.g. change only LSB in lockstep
> from one call to the next, the returned values will be identical. I changed the
> function to this:
>
> uint unpredictableSeed()
> {
> static bool seeded;
> static MinstdRand0 rand;
> if (!seeded) {
> rand.seed(getpid ^ getUTCtime);
> }
> return cast(uint) (getUTCtime ^ rand.next);
> }
>
> It'll be a bit slower but also quite a bit less predictable. But I also removed
> the assert because even with the implementation above, it's not guaranteed that
> two successive returns can't be equal.
>
I meant:
uint unpredictableSeed()
{
static bool seeded;
static MinstdRand0 rand;
if (!seeded) {
rand.seed(getpid ^ getUTCtime);
seeded = true;
}
return cast(uint) (getUTCtime ^ rand.next);
}
It's in SVN.