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...
1条答案
按热度按时间gkl3eglg1#
由于
rand:uniform(17)
返回1到17之间(包括1和17)的数字,因此将其加上3将给予4到20之间的数字。请尝试以下方法:如果
Num1
为3,Num2
为20,则rand:uniform(18) + 2
将变为rand:uniform(18) + 2
,给出一个介于3和20之间的随机数。