Chromium Code Reviews| Index: src/v8.cc |
| =================================================================== |
| --- src/v8.cc (revision 2155) |
| +++ src/v8.cc (working copy) |
| @@ -138,4 +138,27 @@ |
| } |
| +uint32_t V8::Random() { |
| + // Random number generator using George Marsaglia's |
| + // multiply-with-carry algorithm. |
| + static uint32_t hi = 0; |
| + static uint32_t lo = 0; |
|
Dean McNamee
2009/06/15 07:39:25
why not just static uint32_t hi = random() ?
|
| + |
| + // Initialize seed using the system random(). |
| + if (hi == 0) hi = random(); |
| + if (lo == 0) lo = random(); |
| + |
| + // Mix the bits. |
| + hi = 36969 * (hi & 0xFFFF) + (hi >> 16); |
| + lo = 18273 * (lo & 0xFFFF) + (lo >> 16); |
| + return (hi << 16) + (lo & 0xFFFF); |
| +} |
| + |
| + |
| +Smi* V8::RandomPositiveSmi() { |
| + uint32_t random = Random(); |
| + ASSERT(IsPowerOf2(Smi::kMaxValue + 1)); |
| + return Smi::FromInt(random & Smi::kMaxValue); |
| +} |
| + |
| } } // namespace v8::internal |