note 88755 deleted from function.rand by thiago
| From: | thiago@php.net | Date: | Fri, 06 Feb 2009 15:09:07 +0000 |
| Subject: | note 88755 deleted from function.rand by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150306@lists.php.net to get a copy of this message | ||
Note Submitter: szellem dot a dot gepben at gmail dot com
----
Dear "shortschoolbus at live dot com"!
Your code has not worked me, so i have rewrite:
function createRandom($number_of_digits=1,$type=3){
// type: 1 - numeric, 2 - letters, 3 - mixed.
for($x=0;$x<$number_of_digits;$x++){
switch($type){
case "1":
$r = rand(0,9);
break;
case "2":
$r = chr(rand(0,25)+65);
break;
case "3":
$n = rand(0,999);
if($n % 2){
$n = rand(0,999);
if($n % 2){
$r = chr(rand(0,25)+65);
} else {
$r = strtolower(chr(rand(0,25)+65));
}
} else {
$r = rand(0,9);
}
}
$new_string .= $r;
}
return $new_string;
}
I have just tried to use your function, but it has not work well.
First time it hase make just number and all numbers were the same, like
print(createRandom(10));
// 5555555555
I do not know why have you write:
while(substr($num,strlen($num)-1,strlen($num)) == $r){ }
Because this, it hase make just the same characters in the string. When i have removed that, it has
made just numbers,
so the next bug was in the switch case"3":
if(is_numeric(substr($num,strlen($num)-1,strlen($num)))){
I have rewrite it and then it has worked well.
$n = rand(0,999);
if($n % 2){ ...