[Issue 2352] unittest fails randomly

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Sep 10 20:47:43 PDT 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2352





------- Comment #2 from andrei at metalanguage.com  2008-09-10 22:47 -------
(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.


-- 



More information about the Digitalmars-d-bugs mailing list