D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2977 - std.random.unpredictableSeed() should use thread ID somewhere
Summary: std.random.unpredictableSeed() should use thread ID somewhere
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-05-13 13:26 UTC by David Simcha
Modified: 2015-06-09 01:27 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2009-05-13 13:26:07 UTC
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.
Comment 1 David Simcha 2009-12-01 06:21:29 UTC
Fixed SVN.
Comment 2 Walter Bright 2009-12-06 00:50:37 UTC
Fixed dmd 2.037