note 98644 added to function.openssl-random-pseudo-bytes
| From: | Andre at osu1 dot php dot net | Date: | Mon, 28 Jun 2010 23:41:15 +0000 |
| Subject: | note 98644 added to function.openssl-random-pseudo-bytes | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170533@lists.php.net to get a copy of this message | ||
@Typer 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);
}
?>
----
Server IP: 66.207.199.35
Probable Submitter: 99.230.195.22
----
Manual Page -- http://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php
Edit -- https://master.php.net/note/edit/98644
Del: integrated -- https://master.php.net/note/delete/98644/integrated
Del: useless -- https://master.php.net/note/delete/98644/useless
Del: bad code -- https://master.php.net/note/delete/98644/bad+code
Del: spam -- https://master.php.net/note/delete/98644/spam
Del: non-english -- https://master.php.net/note/delete/98644/non-english
Del: in docs -- https://master.php.net/note/delete/98644/in+docs
Del: other reasons-- https://master.php.net/note/delete/98644
Reject -- https://master.php.net/note/reject/98644
Search -- https://master.php.net/manage/user-notes.php