In PHP, how do I generate a big pseudo-random number?

Try the following:

function BigRandomNumber($min, $max) {
  $difference   = bcadd(bcsub($max,$min),1);
  $rand_percent = bcdiv(mt_rand(), mt_getrandmax(), 8); // 0 - 1.0
  return bcadd($min, bcmul($difference, $rand_percent, 8), 0);
}

The math is as following: multiply the difference between the minimum and maximum by a random percentage, and add to the minimum (with rounding to an int).

Leave a Comment