Last active
September 16, 2021 14:05
-
-
Save vip3r011/95e22cc8bd5984b027519fd53b7d0163 to your computer and use it in GitHub Desktop.
randomstuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function randomInt(int $min, int $max) :int | |
{ | |
$getFloat = function() | |
{ | |
$bytes = random_bytes(7); | |
$bytes[6] = $bytes[6] | chr(0xF0); | |
$bytes .= chr(63); // exponent bias (1023) | |
$float = unpack('d', $bytes)[1]; | |
return ($float - 1); | |
}; | |
return (int)(floor($getFloat() * (($max - $min) + 1)) + $min); | |
} | |
function getFloat() :float | |
{ | |
$bytes = random_bytes(7); | |
$bytes[6] = $bytes[6] | chr(0xF0); | |
$bytes .= chr(63); // exponent bias (1023) | |
list(, $float) = unpack('d', $bytes); | |
return ($float - 1); | |
} | |
function getBoolean() :bool | |
{ | |
$byte = random_bytes(1); | |
return (bool) (ord($byte) % 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment