X-Git-Url: http://git.webkit.org/?p=WebKit-https.git;a=blobdiff_plain;f=JavaScriptCore%2Fwtf%2FRandomNumber.cpp;h=e5692271ecc220910b71e090548bffc388fb206e;hp=cbf46f1c44dce53b2cb059e5aa418cb3794f954c;hb=2c77d737a1423b1bc44cfbe9642043f9b5c05a05;hpb=d61fab1f86e5e56ceb3a6fd4d476833305f94336 diff --git a/JavaScriptCore/wtf/RandomNumber.cpp b/JavaScriptCore/wtf/RandomNumber.cpp index cbf46f1..e569227 100644 --- a/JavaScriptCore/wtf/RandomNumber.cpp +++ b/JavaScriptCore/wtf/RandomNumber.cpp @@ -29,7 +29,9 @@ #include "RandomNumberSeed.h" +#include #include +#include #include namespace WTF { @@ -43,17 +45,43 @@ double randomNumber() s_initialized = true; } #endif - + + uint32_t part1; + uint32_t part2; + uint64_t fullRandom; #if COMPILER(MSVC) && defined(_CRT_RAND_S) - unsigned u; - rand_s(&u); - - return static_cast(u) / (static_cast(UINT_MAX) + 1.0); + rand_s(&part1); + rand_s(&part2); + fullRandom = part1; + fullRandom <<= 32; + fullRandom |= part2; #elif PLATFORM(DARWIN) - return static_cast(random()) / (static_cast(RAND_MAX) + 1.0); + part1 = arc4random(); + part2 = arc4random(); + fullRandom = part1; + fullRandom <<= 32; + fullRandom |= part2; +#elif PLATFORM(UNIX) + part1 = random() & (RAND_MAX - 1); + part2 = random() & (RAND_MAX - 1); + // random only provides 31 bits + fullRandom = part1; + fullRandom <<= 31; + fullRandom |= part2; #else - return static_cast(rand()) / (static_cast(RAND_MAX) + 1.0); + part1 = rand() & (RAND_MAX - 1); + part2 = rand() & (RAND_MAX - 1); + // rand only provides 31 bits, and the low order bits of that aren't very random + // so we take the high 26 bits of part 1, and the high 27 bits of part2. + part1 >>= 5; // drop the low 5 bits + part2 >>= 4; // drop the low 4 bits + fullRandom = part1; + fullRandom <<= 27; + fullRandom |= part2; #endif + // Mask off the low 53bits + fullRandom &= (1LL << 53) - 1; + return static_cast(fullRandom)/static_cast(1LL << 53); } }