mardi 4 mai 2021

Erlang: Is there a more elegant way to get a random integer in a specific range, regarding the distribution

My approach is the following:

Num 1 and Num 2 are given numbers that the beginning and end of the range, the resulting random integer can be num 1 or num 2.

Num1 + rand:uniform(Num2-Num1).

Example:

Num 1 = 3 Num 2 = 20

rand:uniform returns a value between 1 and 17. I then add the Num 1 again, so the random value is in the given range.

I see the problem with this implementation is the distribution of my random numbers, because the lowest number in the range has a higher probability, depending on the distance between 0 and num 1 and the range between num 1 and num 2.

I would appreciate a simpler, more elegant solution that distributes the integers uniformly...




Aucun commentaire:

Enregistrer un commentaire