Changeset 40937 in webkit for trunk/JavaScriptCore/wtf


Ignore:
Timestamp:
Feb 12, 2009, 2:28:17 PM (16 years ago)
Author:
[email protected]
Message:

2009-02-12 Geoffrey Garen <[email protected]>

Reviewed by Sam Weinig.


Correctness fix.

  • wtf/RandomNumber.cpp: (WTF::randomNumber): Divide by the maximum representable value, which is different on each platform now, to get values between 0 and 1.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/RandomNumber.cpp

    r40935 r40937  
    4747#endif
    4848   
    49     uint64_t fullRandom;
    5049#if COMPILER(MSVC) && defined(_CRT_RAND_S)
    51     uint32_t part1;
    52     rand_s(&part1);
    53     fullRandom = part1;
     50    uint32_t bits;
     51    rand_s(&bits);
     52    return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
    5453#elif PLATFORM(DARWIN)
    55     fullRandom = arc4random();
     54    uint32_t bits = arc4random();
     55    return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
    5656#elif PLATFORM(UNIX)
    5757    uint32_t part1 = random() & (RAND_MAX - 1);
    5858    uint32_t part2 = random() & (RAND_MAX - 1);
    5959    // random only provides 31 bits
    60     fullRandom = part1;
     60    uint64_t fullRandom = part1;
    6161    fullRandom <<= 31;
    6262    fullRandom |= part2;
     63
     64    // Mask off the low 53bits
     65    fullRandom &= (1LL << 53) - 1;
     66    return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53);
    6367#else
    6468    uint32_t part1 = rand() & (RAND_MAX - 1);
     
    7175    fullRandom <<= 27;
    7276    fullRandom |= part2;
    73 #endif
     77
    7478    // Mask off the low 53bits
    7579    fullRandom &= (1LL << 53) - 1;
    7680    return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53);
     81#endif
    7782}
    7883
Note: See TracChangeset for help on using the changeset viewer.