Changeset 39553 in webkit for trunk/JavaScriptCore/wtf/RandomNumber.cpp
- Timestamp:
- Jan 2, 2009, 12:17:21 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/RandomNumber.cpp
r39516 r39553 46 46 } 47 47 #endif 48 48 49 uint32_t part1; 50 uint32_t part2; 51 uint64_t fullRandom; 49 52 #if COMPILER(MSVC) && defined(_CRT_RAND_S) 50 unsigned u; 51 rand_s(&u); 52 53 return static_cast<double>(u) / (static_cast<double>(UINT_MAX) + 1.0); 53 rand_s(&part1); 54 rand_s(&part2); 55 fullRandom = part1; 56 fullRandom <<= 32; 57 fullRandom |= part2; 54 58 #elif PLATFORM(DARWIN) 55 return static_cast<double>(arc4random()) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0); 59 part1 = arc4random(); 60 part2 = arc4random(); 61 fullRandom = part1; 62 fullRandom <<= 32; 63 fullRandom |= part2; 64 #elif PLATFORM(UNIX) 65 part1 = random() & (RAND_MAX - 1); 66 part2 = random() & (RAND_MAX - 1); 67 // random only provides 31 bits 68 fullRandom = part1; 69 fullRandom <<= 31; 70 fullRandom |= part2; 56 71 #else 57 return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0); 72 part1 = rand() & (RAND_MAX - 1); 73 part2 = rand() & (RAND_MAX - 1); 74 // rand only provides 31 bits, and the low order bits of that aren't very random 75 // so we take the high 26 bits of part 1, and the high 27 bits of part2. 76 part1 >>= 5; // drop the low 5 bits 77 part2 >>= 4; // drop the low 4 bits 78 fullRandom = part1; 79 fullRandom <<= 27; 80 fullRandom |= part2; 58 81 #endif 82 // Mask off the low 53bits 83 fullRandom &= (1LL << 53) - 1; 84 return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53); 59 85 } 60 86
Note:
See TracChangeset
for help on using the changeset viewer.