Skip to content

Instantly share code, notes, and snippets.

@vip3r011
Last active September 16, 2021 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vip3r011/95e22cc8bd5984b027519fd53b7d0163 to your computer and use it in GitHub Desktop.
Save vip3r011/95e22cc8bd5984b027519fd53b7d0163 to your computer and use it in GitHub Desktop.
randomstuff
<?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