note 102007 deleted from function.rand by danbrown
| From: | danbrown@php.net | Date: | Sat, 22 Jan 2011 19:38:53 +0000 |
| Subject: | note 102007 deleted from function.rand by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176010@lists.php.net to get a copy of this message | ||
Note Submitter: X-A
----
hey found out the easiest way to generate random number
thought to share here
//usage random(6) to generate 6 digit random number
function random($length)
{
//collection of random number elements. "total is 36"
$haystack = "abcdefghijklmnopqrstuvwxyz0123456789";
//checking of minimum input length
if($length>0)
{
//creation of empty variable to store the random string
$rand="";
for($i=1; $i<=$length; $i++)
{
//extra seeding of rand funtion
mt_srand((double)microtime() * 1000000);
//geting a number between 1 to 36. 36 = total
$num = mt_rand(1,36);
//picking up the the random charac
$rand .= $haystack{$num};
}
}
//returning it
return $rand;
}
minified version
function random($length){
$haystack = "abcdefghijklmnopqrstuvwxyz0123456789";
if($length>0){
$rand_id="";
for($i=1; $i<=$length; $i++){
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,36);
$rand_id .= $haystack{$num};
}
}