wfRandomString: Correct bias toward digits 1-7
authorKevin Israel <pleasestand@live.com>
Mon, 29 Apr 2013 04:33:06 +0000 (00:33 -0400)
committerKevin Israel <pleasestand@live.com>
Mon, 29 Apr 2013 04:33:06 +0000 (00:33 -0400)
commit34d49b33a597c23fde84078b6f9d0634cccb99a8
tree466913b56272b4742d9fb35ab55b1eb65a1d33e6
parent728dc44c6424f9b8c1fa5b3221fd2acb2901ea96
wfRandomString: Correct bias toward digits 1-7

Two problems with a single line of code:

* dechex() does not include leading zeros, so the first digit
  generated in each iteration cannot be zero unless the return
  value of mt_rand() is 0.

  This also means wfRandomString() is extremely unlikely to
  start a string with '0'.

* mt_rand() does not actually uniformly distribute values over
  [0,2^32-1]; it actually right-shifts one of the bits off (just
  because "[...] the previous php_rand only returns 31 at most"),
  so the maximum value is 0x7fffffff, not 0xffffffff.

  This means wfRandomString() will never start a string with
  any of the digits 8-f.

Including leading zeros and using only the 28 LSBs to form seven
hex digits at a time is the simplest fix.

Change-Id: Ic19b5b97c582485780b24fd35ffef2111cc8b3ca
RELEASE-NOTES-1.22
includes/GlobalFunctions.php