A
A
Anikav2019-03-03 15:25:02
JavaScript
Anikav, 2019-03-03 15:25:02

How to generate numbers with linearly given probability?

Hello.
There is a task to generate numbers in the given range . Moreover, the probability of the appearance of numbers is given, it is not the same.
I will give an example of calling the desired function: Here the range of numbers to generate is from 0 to 20 . Spawn chance 0 - 0%. 10 - 50%, 15 - 10%, 20 - 40%. Accordingly, for example, the number 5 will appear with a 25% probability, the number 1 with a 5% probability, and so on. The number of pairs of values ​​at the input to the function can be any, the range is also any. The generated numbers do not have to be integers. How to implement such a function? Maybe there are ready-made implementations?
random()
5c7bc74df384a515247128.png

PS In the comments, they rightly suggested that the sum of the probabilities on my chart would be more than 100%, which is impossible. Therefore, we can assume that not probability percentages are indicated, but probability units.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Movchan, 2019-03-03
@Anikav

Divide your function by the area below it and get the probability density , take the density integral (not hard for a linear function) and get the distribution function .
Construct the inverse function of the distribution function, generate a number uniformly distributed from 0 to 1 and calculate the value of the resulting function.

R
rPman, 2019-03-03
@rPman

If the parameters set the probability discretely (in the picture, by the way, you don’t have discretely, but complex nonlinear dependencies, i.e. 1 occurs almost 50 times less often than 10, I dare to assume that you don’t need SO) on intervals, such as from [0-10) - 50 then solve the problem head-on, first choose the interval according to the probabilities (if you set it quantitatively, then this is the sum of the given values ​​\u200b\u200b- the maximum value, and the rand value interval is the corresponding value for the sum up to this interval and with it, after choosing the interval, just repeat rand because within the interval you need uniform.

P
polybook, 2019-03-07
@polybook

Generate a pair of random numbers with a uniform distribution. Suppose the first x=10. If the second lies under your chart, i.e. at

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question