note 88825 rejected from function.rand by thiago
| From: | thiago@php.net | Date: | Tue, 10 Feb 2009 02:53:57 +0000 |
| Subject: | note 88825 rejected from function.rand by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150436@lists.php.net to get a copy of this message | ||
Note Submitter: cndingo at qq dot com
----
<?php
function getRand($min,$max) {
$num=mt_rand($min,$max);
return mt_rand($min,$num);
}
function totalStr($haystack, $needle,$i = 0){
while(strpos($haystack,$needle) !== false) {
$haystack = substr($haystack, (strpos($haystack,$needle) + 1));
$i++;
}
return $i;
}
for ($i=0;$i<=1000;$i++){
$str .= getRand(0,9);
}
for ($i=0;$i<=9;$i++){
echo totalStr($str,"$i")."\n";
}
?>
it printed
304
185
146
121
79
59
49
35
14
9
and I changed.
<?php
function getRand($min,$max) {
$num=mt_rand($min,$max);
return mt_rand($min,$num);
}
function totalStr($haystack, $needle,$i = 0){
while(strpos($haystack,$needle) !== false) {
$haystack = substr($haystack, (strpos($haystack,$needle) + 1));
$i++;
}
return $i;
}
for ($i=0;$i<=1000;$i++){
$str .= getRand(10,19); //change 0,9 to 10,19
}
for ($i=0;$i<=9;$i++){
echo totalStr($str,"$i")."\n";
}
?>
and it printed
282
372
135
115
102
66
44
44
16
11
whay??