note 102007 added to function.rand
| From: | X-A at osu1 dot php dot net | Date: | Sat, 22 Jan 2011 19:14:26 +0000 |
| Subject: | note 102007 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176009@lists.php.net to get a copy of this message | ||
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};
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 117.195.101.21
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/102007
Del: integrated -- https://master.php.net/note/delete/102007/integrated
Del: useless -- https://master.php.net/note/delete/102007/useless
Del: bad code -- https://master.php.net/note/delete/102007/bad+code
Del: spam -- https://master.php.net/note/delete/102007/spam
Del: non-english -- https://master.php.net/note/delete/102007/non-english
Del: in docs -- https://master.php.net/note/delete/102007/in+docs
Del: other reasons-- https://master.php.net/note/delete/102007
Reject -- https://master.php.net/note/reject/102007
Search -- https://master.php.net/manage/user-notes.php