note 107712 added to function.rand
| From: | matthias dot isler at gmail dot com | Date: | Tue, 28 Feb 2012 15:44:53 +0000 |
| Subject: | note 107712 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186413@lists.php.net to get a copy of this message | ||
I had to create a function that generates a random binominal distributed integer. Take a look at the
following Wiki article:
http://en.wikipedia.org/wiki/Binomial_distribution
Here is my solution:
<?php
function bin_rand($min = null, $max = null)
{
$min = ($min) ? (int) $min : 0;
$max = ($max) ? (int) $max : PHP_INT_MAX;
$range = range($min, $max);
$average = array_sum($range) / count($range);
$dist = array();
for ($x = $min; $x <= $max; $x++) {
$dist[$x] = -abs($average - $x) + $average + 1;
}
$map = array();
foreach ($dist as $int => $quantity) {
for ($x = 0; $x < $quantity; $x++) {
$map[] = $int;
}
}
shuffle($map);
return current($map);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 46.126.104.94
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/107712
Del: integrated -- https://master.php.net/note/delete/107712/integrated
Del: useless -- https://master.php.net/note/delete/107712/useless
Del: bad code -- https://master.php.net/note/delete/107712/bad+code
Del: spam -- https://master.php.net/note/delete/107712/spam
Del: non-english -- https://master.php.net/note/delete/107712/non-english
Del: in docs -- https://master.php.net/note/delete/107712/in+docs
Del: other reasons-- https://master.php.net/note/delete/107712
Reject -- https://master.php.net/note/reject/107712
Search -- https://master.php.net/manage/user-notes.php