Just a thought... which of these is "better"?

Raimo Niskanen raimo+erlang-questions@REDACTED
Tue Oct 26 09:16:24 CEST 2021


The 'rand' module is better.  The 'random' module implements
an old, statistically flawed Pseudo-Random Number Generator
and should not be used any more.
:
:
:
Joking aside; I guess you are saying that the verbose exception from
`rand:uniform/1` does not give any better understanding of the error,
which is that the argument `0` is out of range since it has to be
an `integer() >= 1`.

I agree that you have a point.  But introducing guards at every
API function that is a wrapper for another API function would just
be introducing redundant guards that may get out of sync with
each other in a future code update.

The actual problem here I'd say is that the exception from
`rand:uniform_s/2` is what you show here.

I think a good solution would be to implement
[`erl_error`](https://erlang.org/doc/man/erl_error.html)
exception formatting for the `rand` module.  Such an
implementation could easily add a line:

    *** argument 1: not of type integer() >= 1

to that exception printout.


On Mon, Oct 25, 2021 at 06:01:25PM -0500, Steve Davis wrote:
> > rand:uniform(0).
> ** exception error: no function clause matching 
>                     rand:uniform_s(0,
>                                    {#{bits => 58,
>                                       jump => #Fun<rand.3.47293030>,
>                                       next => #Fun<rand.0.47293030>,
>                                       type => exsss,
>                                       uniform => #Fun<rand.1.47293030>,
>                                       uniform_n => #Fun<rand.2.47293030>},
>                                     [287343104968817679|100345946487370353]}) (rand.erl, line 326)
>      in function  rand:uniform/1 (rand.erl, line 299)
> > random:uniform(0).
> ** exception error: no function clause matching random:uniform(0) (random.erl, line 119)

Cheers
-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list