public static function randomInt(int $min, int $max): int
    {
        if ($max <= $min) {
            throw new \Exception('Minimum equal or greater than maximum!');
        }
        if ($max < 0 || $min < 0) {
            throw new \Exception('Only positive integers supported for now!');
        }

        $difference = $max - $min;
        for ($power = 8; pow(2, $power) < $difference; $power = $power * 2) {
        }
        $powerExp = $power / 8;
        do {
            $randDiff = hexdec(bin2hex(self::randomBytes($powerExp)));
        } while ($randDiff > $difference);
        return $min + $randDiff;
    }
public static function randomInt(int $min, int $max): int
    {
        if ($max <= $min) {
            throw new \Exception('Minimum equal or greater than maximum!');
        }
        if ($max < 0 || $min < 0) {
            throw new \Exception('Only positive integers supported for now!');
        }

        $difference = $max - $min;
        for ($power = 8; pow(2, $power) < $difference; $power = $power * 2) {
        }
        $powerExp = $power / 8;
        do {
            $randDiff = hexdec(bin2hex(self::randomBytes($powerExp)));
        } while ($randDiff > $difference);
        return $min + $randDiff;
    }
public static function randomInt ( int $ min , int $ max ) : int { if ( $ max <= $ min ) { throw new \ Exception ( 'Minimum equal or greater than maximum!' ) ; } if ( $ max < 0 || $ min < 0 ) { throw new \ Exception ( 'Only positive integers supported for now!' ) ; } $ difference = $ max - $ min ; for ( $ power = 8 ; pow ( 2 , $ power ) < $ difference ; $ power = $ power * 2 ) { } $ powerExp = $ power / 8 ; do { $ randDiff = hexdec ( bin2hex ( self :: randomBytes ( $ powerExp ) ) ) ; } while ( $ randDiff > $ difference ) ; return $ min + $ randDiff ; }
Returns a random integer between the provided min and max using random
bytes. Throws exception if min and max arguments have inconsistencies.

@param int $min
@param int $max
@throws \Exception
@return int
Returns a random integer between the provided min and max using random bytes . Throws exception if min and max arguments have inconsistencies .