[erlang-questions] On OTP rand module difference between OTP 19 and OTP 20

Michael Truog <>
Thu Aug 31 20:35:24 CEST 2017


On 08/31/2017 05:30 AM, Raimo Niskanen wrote:
> On Thu, Aug 31, 2017 at 10:34:16AM +1200, Richard A. O'Keefe wrote:
>>
>> On 30/08/17 6:42 PM, Raimo Niskanen wrote:
>>> On Wed, Aug 30, 2017 at 11:44:57AM +1200, Richard A. O'Keefe wrote:
>>>> There are applications of random numbers for which it is important
>>>> that 0 never be returned.  Of course, nothing stops me writing
>>> What kind of applications?  I would like to get a grip on how needed this
>>> function is?
>> I'll give you just one example.  There are two ways I know to generate
>> normally distributed random numbers.  One of them goes like this:
>>
>>       sqrt(-2 * ln(randnz()) * cos(pi * random())
>>
>> where random() is in [0,1) but randnz() must be in (0,1).
>>
>> OK, I'll give you another example.  This is part of an algorithm for
>> generating gamma variates, one of the best known.
>>
>>       U <- random()
>>       if U <= r then
>>           z <- -ln(U/r)
>>       else
>>           z <- ln(random()/lambda)
>>       end
>>
>> You will notice that both of the calls to ln will go wrong if
>> random() can return 0.
>>
>> These aren't the only examples, but I have an appointment.
> Thank you!
>
> Should I make a pull request of this?
>
>      https://github.com/erlang/otp/compare/OTP-20.0...RaimoNiskanen:raimo/stdlib/rand-uniformNZ
>
> Is the name uniformNZ good enough?
> Are uniform floats complete enough with this addition?

As I argued in the original pull request for these recent 20.0 random number changes, a uniform distribution is much more intuitive if it is inclusive: [0,1]

For example, if you are dealing with probabilities, it is simpler to think in percentages from 0.00 to 1.00

An example from the python documentation is at https://docs.python.org/3/library/random.html#random.uniform though they have ambiguity about the highest value due to a rounding problem they have.

I have had my own dependency for uniform as [0,1] at https://github.com/okeuday/quickrand/blob/fc5e21ec70ee94dd4ce1c5ee02b55ceea03f9008/src/quickrand.erl#L294-L309 so I have been working around this absence.  Though I would assume other people would benefit from the addition of a [0,1] function in Erlang/OTP.

Best Regards,
Michael





More information about the erlang-questions mailing list