note 108707 added to function.rand
| From: | Simon | Date: | Sat, 19 May 2012 11:29:44 +0000 |
| Subject: | note 108707 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188069@lists.php.net to get a copy of this message | ||
I've written my own rand()-function, it also works very fine:
<?php
function random($min, $max) {
// md5() generates a hexadecimal number, so we must convert it into base 10
$rand = base_convert( md5( microtime() ), 16, 10);
// the modulus operator doesn't work with great numbers, so we have to cut the number
$rand = substr($rand, 10, 6);
$diff = $max - $min + 1;
return ($rand % $diff) + $min;
}
// test:
$arr = array();
for ($i = 0; $i < 1000; $i++) {
$arr[random(1, 10)]++;
}
for ($i = 1; $i <= 10; $i++)
echo '<br />' . $i . ': ' . $arr[$i] . "\n";
?>
expected result would be about 100, my result:
1: 108
2: 100
3: 105
4: 92
5: 101
6: 88
7: 93
8: 114
9: 102
10: 97
----
Server IP: 192.35.244.50
Probable Submitter: 83.215.58.31
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/108707
Del: integrated -- https://master.php.net/note/delete/108707/integrated
Del: useless -- https://master.php.net/note/delete/108707/useless
Del: bad code -- https://master.php.net/note/delete/108707/bad+code
Del: spam -- https://master.php.net/note/delete/108707/spam
Del: non-english -- https://master.php.net/note/delete/108707/non-english
Del: in docs -- https://master.php.net/note/delete/108707/in+docs
Del: other reasons-- https://master.php.net/note/delete/108707
Reject -- https://master.php.net/note/reject/108707
Search -- https://master.php.net/manage/user-notes.php