vendredi 15 avril 2016

PHP - is this script a good solution for random numbers?

Hi I found this script.

The script:

function urandom_rand($min = 0, $max = 0x7FFFFFFF){
$min = (int)$min;
$max = (int)$max;
if ($max <= $min)
    trigger_error('Minimum value must be greater than maximum value.');
if ($min < 0 || $max > 0x7FFFFFFF)
    trigger_error('Values must be between 0 and 2147483647.');

$M = bcadd(0x7FFFFFFF,1); // (up bound of iv)+1
$N = bcadd($max-$min, 1); // how many different values this function can return
$h = bcmod($M, $N); // the last h integers from unpack are "invalids"

do{
    $bytes = mcrypt_create_iv(4, MCRYPT_DEV_URANDOM);
    $r = unpack("N", $bytes)[1] & 0x7FFFFFFF;
} while ($r > ($M-$h));
return (bcmod($r, $N) + $min);
}

My question is: Is this script a good solution for the generation of random numbers?




Aucun commentaire:

Enregistrer un commentaire