- bin2hex: documentation ( source)
- log: documentation ( source)
- max: documentation ( source)
- min: documentation ( source)
- hexdec: documentation ( source)
<?php
function generateRandomNumber($min = 0, $max = PHP_INT_MAX)
{
$range = max($max, $min) - min($max, $min);
if (0 == $range) {
throw new \Exception('Cannot create a random number with range = 0 ($min and $max are the same).');
}
$byteLength = (int) (log($range, 2) / 8) + 1;
$random = hexdec(bin2hex(openssl_random_pseudo_bytes($byteLength))) % $range;
return $min + $random;
}
echo generateRandomNumber();