Reputation: 103
I'm looking for a function to generate random numbers in [a,b]
Function must take one argument - seed and return random x є [a,b]
.
The probability of x = a
is the lowest, but is increasing when moving to b. The probability of x = b
should be the highest.
As far as I understand what I'm looking for is an implementation of cumulative distribution function, but can't say for sure. So i'll be glad for direct literature. Or maybe there's an in-box php implementation already?
Thanks
Upvotes: 0
Views: 167
Reputation: 570
First of all, you did not specify what an argument should be. The function below does not use comulative distribution. Incereasing the $n will cause values close to b more likely to be returned.
function almostRandom() {
$a = 1;
$b = 10;
$n = 2.5;
$random = rand(0, 1000000) / 1000000;
$positionInInterval = 1 - pow($random, $n);
$intervalLength = $b - $a;
$value = $a + $intervalLength * $positionInInterval;
return $value;
}
Upvotes: 1