This is more of a maths/general programming question, but I am programming with PHP is that makes a difference.
I think the easiest way to explain is with an example.
If the range is between 1 and 10.
I want to generate a number that is between 1 an 10 but is more likely lower than high.
The only way I can think is generate an array with 10 elements equal to 1, 9 elements equal to 2, 8 elements equal to 3.....1 element equal to 10. Then generate a random number based on the number of elements.
The trouble is I am potentially dealing with 1 - 100000 and that array would be ridiculously big.
So how best to do it?
Use Math. Math. random() returns a double type pseudo-random number, greater than or equal to zero and less than one.
According to the video, if you ask people to randomly pick any one integer between 1 and 10 (both inclusive), people are more likely to choose 7.
In general, generating a random number from a probability distribution means transforming random numbers so that the numbers fit the distribution. Perhaps the most generic way to do so is called inverse transform sampling: Generate a uniform random number in [0, 1].
Generate a random number between 0 and a random number!
Generate a number between 1 and foo(n), where foo runs an algorithm over n (e.g. a logarithmic function). Then reverse foo() on the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With