note 66679 added to function.rand
| From: | atom14x7 at gmail dot com | Date: | Thu, 25 May 2006 20:28:00 +0000 |
| Subject: | note 66679 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-112284@lists.php.net to get a copy of this message | ||
www.random.org gives a random number based on radioactive activity
a simple script to retrieve (a) random number(s) from random.org is as follows:
<?php
function get_random_no ($min=0,$max=255,$num=1) {
//if $num > 1, expect array returned
//random number courtesy of random.org
$url = '"http://www.random.org/cgi-bin/randnum';
@$file = file("$url?min=$min&max=$max&num=$num&col=1");
if (!$file) { //could not connect
if ($num==1) {
srand((double)microtime()*intval(rand(1,1000000)));
$return = intval(rand($min, $max));
}
else {
for ($i=0;$i<$num;$i++) {
srand((double)microtime()*intval(rand(1,1000000)));
$return[$i] = intval(rand($min, $max));
}
}
}
elseif ($num==1)
$return = intval($file[0]);
else {
foreach ($file as $i => $f)
$return[] = intval($f);
}
return $return;
}
?>
If $num == 1, expect an integer to be returned. Else an array will be returned
If the script can't connect to random.org, a locally generated random number will be used
instead
The $min and $max arguments are the same as in rand(), except for the fact that is $max is ommitted,
it defaults to 255, not RAND_MAX (pretty obviously)
----
Server IP: 85.116.0.130
Probable Submitter: 84.71.19.115
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- http://master.php.net/note/edit/66679
Del: integrated -- http://master.php.net/note/delete/66679/integrated
Del: useless -- http://master.php.net/note/delete/66679/useless
Del: bad code -- http://master.php.net/note/delete/66679/bad+code
Del: spam -- http://master.php.net/note/delete/66679/spam
Del: non-english -- http://master.php.net/note/delete/66679/non-english
Del: in docs -- http://master.php.net/note/delete/66679/in+docs
Del: other reasons-- http://master.php.net/note/delete/66679
Reject -- http://master.php.net/note/reject/66679
Search -- http://master.php.net/manage/user-notes.php