$max) { 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)) { 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); } /** * Get the big endian two's complement of a given big integer in * binary notation * * @param string $long * @return string */ public function btwoc($long) { if (ord($long[0]) > 127) { return "\x00" . $long; } return $long; } /** * Translate a binary form into a big integer string * * @param string $binary * @return string */ public function fromBinary($binary) { return $this->_math->binaryToInteger($binary); } /** * Translate a big integer string into a binary form * * @param string $integer * @return string */ public function toBinary($integer) { return $this->_math->integerToBinary($integer); } }