Created
June 2, 2016 14:45
-
-
Save anonymous/87e3ae6fd3320447f46b973e75c2ea85 to your computer and use it in GitHub Desktop.
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 crypto_rand_secure($min, $max) | |
{ | |
$range = $max - $min; | |
if ($range < 1) return $min; // not so random... | |
$log = ceil(log($range, 2)); | |
$bytes = (int) ($log / 8) + 1; // length in bytes | |
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes))); | |
return $min + $rnd%$range; | |
} | |
$min = 0; | |
$max = 100; | |
//set up an array to store results | |
$distribution = array(); | |
//initialize array to 0 | |
for($i = 0; $i < $max; $i++) { | |
$distribution[$i] = 0; | |
} | |
//run the test | |
for($i=0; $i < 1000000; $i++) { | |
$result = crypto_rand_secure(0,100); | |
$distribution[$result]++; | |
} | |
var_dump($distribution); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment