note 66679 deleted from function.rand by bjori
| From: | bjori@php.net | Date: | Thu, 25 May 2006 23:34:00 +0000 |
| Subject: | note 66679 deleted from function.rand by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-112295@lists.php.net to get a copy of this message | ||
Note Submitter: atom14x7 at gmail dot com
Reason: useless
----
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)