Fix Issue 19138 - std.uuid.randomUUID should not depend on std.random.Random being Mt19937

This commit is contained in:
Nathan Sashihara 2018-08-03 12:06:29 -04:00
parent 9e1870c053
commit 202a837b7d

View file

@ -1210,7 +1210,25 @@ public struct UUID
@safe UUID randomUUID() @safe UUID randomUUID()
{ {
import std.random : rndGen; import std.random : rndGen;
return randomUUID(rndGen); // A PRNG with fewer than `n` bytes of state cannot produce
// every distinct `n` byte sequence.
static if (typeof(rndGen).sizeof >= UUID.sizeof)
{
return randomUUID(rndGen);
}
else
{
import std.random : unpredictableSeed, Xorshift192;
static assert(Xorshift192.sizeof >= UUID.sizeof);
static Xorshift192 rng;
static bool initialized;
if (!initialized)
{
rng.seed(unpredictableSeed);
initialized = true;
}
return randomUUID(rng);
}
} }
/// ditto /// ditto
@ -1260,18 +1278,6 @@ if (isInputRange!RNG && isIntegral!(ElementType!RNG))
auto uuid3 = randomUUID(gen); auto uuid3 = randomUUID(gen);
} }
/*
* Original boost.uuid used Mt19937, we don't want
* to use anything worse than that. If Random is changed
* to something else, this assert and the randomUUID function
* have to be updated.
*/
@safe unittest
{
import std.random : rndGen, Mt19937;
static assert(is(typeof(rndGen) == Mt19937));
}
@safe unittest @safe unittest
{ {
import std.random : Xorshift192, unpredictableSeed; import std.random : Xorshift192, unpredictableSeed;