public static function randInteger($min, $max, $strong = false)
{
if ($min > $max) {
require_once 'Zend/Crypt/Exception.php';
throw new Zend_Crypt_Exception('The min parameter must be lower than max parameter');
}
$range = $max - $min;
if ($range == 0) {
return $max;
} elseif ($range > PHP_INT_MAX || is_float($range)) {
require_once 'Zend/Crypt/Exception.php';
throw new Zend_Crypt_Exception('The supplied range is too great to generate');
}
if (function_exists('random_int')) {
// available in PHP 7
return random_int($min, $max);
}
// calculate number of bits required to store range on this machine
$r = $range;
$bits = 0;
while ($r) {
$bits++;
$r >>= 1;
}
$bits = (int) max($bits, 1);
$bytes = (int) max(ceil($bits / 8), 1);
$filter = (int) ((1 << $bits) - 1);
do {
$rnd = hexdec(bin2hex(self::randBytes($bytes, $strong)));
$rnd &= $filter;
} while ($rnd > $range);
return $min + $rnd;
}