Answer the question
In order to leave comments, you need to log in
How to generate a random number, the larger the number, the less chance?
It is necessary to generate a random number in a certain range, while the chance for a larger number should be less (The larger the number, the less the chance of it falling out)
Answer the question
In order to leave comments, you need to log in
Uh, a problem from school mathematics, I would like to remember more.
If I think correctly, we need to take the probability density function, integrate over a given interval, take the inverse function from the integral and substitute the arguments with a uniform distribution generator for this function.
Well, that is, if you have a linear decrease in the probability f (x) = -x, it will be a rotated square root on a given interval (something like, count who has not forgotten the integrals from school yet).
In general it turns out:
/**
* Generates a random number between 0.00 and 1.00
*/
function generateNum()
{
return 1 - sqrt(rand(0, 1000)/1000);
}
$nums = [];
for($i = 0; $i < 10000; ++$i) {
$index = sprintf('%.1f', floor(generateNum() * 10) / 10);
$nums[$index] = ($nums[$index] ?? 0) + 1;
}
ksort($nums);
var_dump($nums);
If you multiply two random numbers, then you will no longer get a linear distribution, but a Pearson distribution.
You can play around with the numbers by adding some coefficients.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question