Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2016 14:45
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 anonymous/87e3ae6fd3320447f46b973e75c2ea85 to your computer and use it in GitHub Desktop.
Save anonymous/87e3ae6fd3320447f46b973e75c2ea85 to your computer and use it in GitHub Desktop.
<?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