note 98645 added to function.openssl-random-pseudo-bytes

From: Date: Mon, 28 Jun 2010 23:41:38 +0000
Subject: note 98645 added to function.openssl-random-pseudo-bytes
Groups: php.notes 
Request: Send a blank email to php-notes+get-170534@lists.php.net to get a copy of this message
@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); } ?> ---- 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/98645 Del: integrated -- https://master.php.net/note/delete/98645/integrated Del: useless -- https://master.php.net/note/delete/98645/useless Del: bad code -- https://master.php.net/note/delete/98645/bad+code Del: spam -- https://master.php.net/note/delete/98645/spam Del: non-english -- https://master.php.net/note/delete/98645/non-english Del: in docs -- https://master.php.net/note/delete/98645/in+docs Del: other reasons-- https://master.php.net/note/delete/98645 Reject -- https://master.php.net/note/reject/98645 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170534) next »