Bug 2352 – unittest fails randomly

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-09-10T18:17:00Z
Last change time
2015-06-09T01:20:16Z
Assigned to
bugzilla
Creator
bartosz

Comments

Comment #0 by bartosz — 2008-09-10T18:17:23Z
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.