Ignore:
Timestamp:
Sep 13, 2007, 11:52:29 AM (18 years ago)
Author:
ggaren
Message:

2007-09-12 Geoff Garen <[email protected]>

Reviewed by Sam Weinig.

Fixed <rdar://problem/5429064> 141885 Safari JavaScript: Math.random() slightly less randomly distributed than on Safari / Mac

Math.random was skewed slightly upward because it assumed that RAND_MAX was outside the range of
values that rand() might return. This problem was particularly pronounced on Windows because
the range of values returned by rand() on Windows is 216 smaller than the range of values
return by rand() on Mac.

Fixed by accounting for RAND_MAX return values. Also, switched Windows over to rand_s, which has
a range that's equal to rand()'s range on Mac.

  • kjs/config.h:
  • kjs/math_object.cpp: (MathFuncImp::callAsFunction): Use the new new thing.
  • wtf/MathExtras.h: Platform abstraction for random numbers, to cover over differences on Windows. (wtf_random_init): (wtf_random):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/math_object.cpp

    r23930 r25541  
    116116// ------------------------------ MathObjectImp --------------------------------
    117117
    118 static bool randomSeeded = false;
     118static bool didInitRandom;
    119119
    120120MathFuncImp::MathFuncImp(ExecState* exec, int i, int l, const Identifier& name)
     
    206206    break;
    207207  case MathObjectImp::Random:
    208       if (!randomSeeded) {
    209           srand(static_cast<unsigned>(time(0)));
    210           randomSeeded = true;
     208      if (!didInitRandom) {
     209          wtf_random_init();
     210          didInitRandom = true;
    211211      }
    212       result = (double)rand() / RAND_MAX;
     212      result = wtf_random();
    213213      break;
    214214  case MathObjectImp::Round:
Note: See TracChangeset for help on using the changeset viewer.