Bug 2977 – std.random.unpredictableSeed() should use thread ID somewhere
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-05-13T13:26:00Z
Last change time
2015-06-09T01:27:58Z
Assigned to
andrei
Creator
dsimcha
Comments
Comment #0 by dsimcha — 2009-05-13T13:26:07Z
uint unpredictableSeed()
{
static bool seeded;
static MinstdRand0 rand;
if (!seeded) {
rand.seed(getpid ^ cast(uint)getUTCtime);
seeded = true;
}
rand.popFront;
return cast(uint) (getUTCtime ^ rand.front);
}
If called from multiple threads at exactly the same time, unpredictableSeed gives every thread the same seed. This is annoying when running the same monte carlo simulation simultaneously in multiple threads, since I tend to spawn all of the threads at exactly the same time. Something like:
(getpid + cast(uint) Thread.getThis) ^ cast(uint) getUTCtime
might work well.