note 19541 modified in function.mt-rand by bjori
| From: | bjori@php.net | Date: | Sun, 16 Apr 2006 12:22:23 +0000 |
| Subject: | note 19541 modified in function.mt-rand by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108251@lists.php.net to get a copy of this message | ||
<?php
//
// Generates a random string with the specified length
// Chars are chosen from the provided [optional] list
//
function simpleRandString($length=16, $list="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"){
mt_srand((double)microtime()*1000000);
$newstring="";
if($length>0){
while(strlen($newstring)<$length){
$newstring.=$list[mt_rand(0, strlen($list)-1)];
}
}
return $newstring;
}
//
// Generates a random string with the specified length
// Includes: a-z, A-Z y 0-9
//
function randString($length=16) {
$newstring="";
if($length>0) {
while(strlen($newstring)<$length) {
$randnum = mt_rand(0,61);
if ($randnum < 10) {
$newstring.=chr($randnum+48);
} elseif ($randnum < 36) {
$newstring.=chr($randnum+55);
} else {
$newstring.=chr($randnum+61);
}
}
}
return $newstring;
}
?>
--was--
//
// Generates a random string with the specified length
// Chars are chosen from the provided [optional] list
//
function simpleRandString($length=16, $list="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"){
mt_srand((double)microtime()*1000000);
$newstring="";
if($length>0){
while(strlen($newstring)<$length){
$newstring.=$list[mt_rand(0, strlen($list)-1)];
}
}
return $newstring;
}
//
// Generates a random string with the specified length
// Includes: a-z, A-Z y 0-9
//
function randString($length=16){
mt_srand((double)microtime()*1000000);
$newstring="";
if($length>0){
while(strlen($newstring)<$length){
switch(mt_rand(1,3)){
case 1: $newstring.=chr(mt_rand(48,57)); break; // 0-9
case 2: $newstring.=chr(mt_rand(65,90)); break; // A-Z
case 3: $newstring.=chr(mt_rand(97,122)); break; // a-z
}
}
}
return $newstring;
}
http://php.net/manual/en/function.mt-rand.php