note 98645 deleted from function.openssl-random-pseudo-bytes by danbrown

From: Date: Tue, 29 Jun 2010 12:23:46 +0000
Subject: note 98645 deleted from function.openssl-random-pseudo-bytes by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170541@lists.php.net to get a copy of this message
Note Submitter: Andre ---- @Tyler Larson I tried the function you posted but found during testing that a min value of 10 and max value of 11 would always result in a number of 10. I incremented the $range by 1 to overcome this. Here's the modified code. <?php function crypto_rand($min, $max) { $range = $max - $min; if ($range == 0) return $min; // not so random... $range++; $length = (int) (log($range,2) / 8) + 1; return $min + (hexdec(bin2hex(openssl_random_pseudo_bytes($length,$s))) % $range); } ?>

« previous php.notes (#170541) next »