Bug 18596 – std.random.unpredictableSeed could use something better than MinstdRand0

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-03-12T09:51:09Z
Last change time
2018-03-22T22:49:33Z
Assigned to
No Owner
Creator
Nathan S.

Comments

Comment #0 by n8sh.secondary — 2018-03-12T09:51:09Z
Currently `std.random.unpredictableSeed` returns the result of a thread-local MinstdRand0 instance xor'd against the clock. MinstdRand0 is slow (due to integer division) and somewhat outdated. A particular weakness of using MinstdRand0 is that it is very likely that consecutive calls to `unpredictableSeed` will return numbers that are identical in the high bit, since MinstdRand0 only produces results in the range `1 .. 2 ^^ 31 - 1`. There are modern PRNG algorithms that have comparable state size to MinstdRand0 (64 bits or 32 bits) but are faster than MinstdRand0 and have output that scores better on randomness tests like BigCrush. On some platforms we can use functions like `arc4random` which incorporate system entropy and remove the need to roll our own entropy-gathering function to set an initial state for a PRNG.
Comment #1 by n8sh.secondary — 2018-03-15T15:58:28Z
Comment #2 by github-bugzilla — 2018-03-22T22:49:32Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/f39686c771955c912273f9f15d2ae37862f885a8 Partially Fix Issue 18596: use arc4random when available for unpredictableSeed https://github.com/dlang/phobos/commit/b87d28f2c77e0d56e313fa120e3cab2198dde24f Merge pull request #6267 from n8sh/unpredictableSeedOf-arc4random Partially Fix Issue 18596: use arc4random when available for unpredictableSeed merged-on-behalf-of: unknown