Thread overview
[Issue 2352] New: unittest fails randomly
Sep 10, 2008
d-bugmail
Sep 11, 2008
d-bugmail
Sep 11, 2008
d-bugmail
September 10, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2352

           Summary: unittest fails randomly
           Product: D
           Version: 2.019
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: bartosz@relisoft.com


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.


-- 

September 10, 2008
On Wed, Sep 10, 2008 at 7:17 PM,  <d-bugmail@puremagic.com> wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=2352
>
>           Summary: unittest fails randomly
>           Product: D
>           Version: 2.019
>          Platform: PC
>        OS/Version: Windows
>            Status: NEW
>          Severity: normal
>          Priority: P2
>         Component: Phobos
>        AssignedTo: bugzilla@digitalmars.com
>        ReportedBy: bartosz@relisoft.com
>
>
> 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.
>
>
> --
>
>

Looks like the unpredictable seed... isn't so unpredictable!

Sorry.
September 11, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2352


andrei@metalanguage.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #1 from andrei@metalanguage.com  2008-09-10 22:28 -------
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.


-- 

September 11, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2352





------- Comment #2 from andrei@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.


--