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

Raimo Niskanen <>
Wed Aug 30 08:42:02 CEST 2017


On Wed, Aug 30, 2017 at 11:44:57AM +1200, Richard A. O'Keefe wrote:
> 
> 
> On 29/08/17 8:35 PM, Raimo Niskanen wrote:
> >
> > Regarding the changed uniform float behaviour: it is the functions
> > rand:uniform/0 and rand:uniform_s/1 this concerns.  They were previously
> > (OTP-19.3) documented to output a value 0.0 < X < 1.0 and are now
> > (OTP-20.0) documented to return 0.0 =< X < 1.0.
> 
> 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?

> 
> uniform_nonzero() ->
>      X = rand:uniform(),
>      if X >  0.0 -> X
>       ; X =< 0.0 -> uniform_nonzero()
>      end.
> 
> but it would be nice to have this already in the library.
> 
> (I know the old library had the same gap.  But with such a major
> reworking of random number generation, it's time to close it.)

We chose 0.0 =< X < 1.0 because it was most like the integer generator i.e
including the lower bound but excluding the upper.  And as you say it is
easy to exclude the lower bound.

If we would implement 0.0 < X < 1.0, would then 0.0 =< X =< 1.0 also be
missing, and for completeness 0.0 < X =< 1.0?
Which of the four are worth implementing?

You clould argue that including both bounds is the most general
because it is easy to retry if you want to exclude a value.

Maybe something like:
    uniform(Opts) -> float()
    uniform_s(Opts, State) -> float()
        Opts :: [(exclude_zero | exclude_one)]


> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list