note 88755 added to function.rand
| From: | szellem dot a dot gepben at gmail dot com | Date: | Fri, 06 Feb 2009 08:13:31 +0000 |
| Subject: | note 88755 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150289@lists.php.net to get a copy of this message | ||
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){ ...
----
Server IP: 92.43.200.200
Probable Submitter: 77.234.65.108
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/88755
Del: integrated -- https://master.php.net/note/delete/88755/integrated
Del: useless -- https://master.php.net/note/delete/88755/useless
Del: bad code -- https://master.php.net/note/delete/88755/bad+code
Del: spam -- https://master.php.net/note/delete/88755/spam
Del: non-english -- https://master.php.net/note/delete/88755/non-english
Del: in docs -- https://master.php.net/note/delete/88755/in+docs
Del: other reasons-- https://master.php.net/note/delete/88755
Reject -- https://master.php.net/note/reject/88755
Search -- https://master.php.net/manage/user-notes.php