[erlang-bugs] Unstated limits of random:uniform/1

Kostis Sagonas kostis@REDACTED
Tue Feb 11 15:47:25 CET 2014


The documentation of random:uniform/1 reads:

   uniform(N) -> integer() >= 1

   Types:

     N = integer() >= 1

   Given an integer N >= 1, uniform/1 returns a random integer uniformly 
distributed between 1 and N, updating the state in the process dictionary.

and from it a (naive?) Erlang programmer would assume that it works for 
Erlang integers. However, apparently there is an (unstated) upper limit.

========================================================================
Eshell V6.0  (abort with ^G)
1> random:uniform(1 bsl 42).
1950905779137
2> random:uniform(1 bsl 1023).
64990220693815292632299777453770053245701880982584490305757715776780176648584151835529728245903303858071465265235635364507930685677056366431569479144084789774752709050314473717035731429737215919311815680621634352115003928201262448305879457258028874562676857884269587024825648343920396535221283000212783104001
3> random:uniform(1 bsl 1024).
** exception error: an error occurred when evaluating an arithmetic 
expression
      in function  random:uniform/1
      in call from erl_eval:do_apply/6
      in call from shell:exprs/7
      in call from shell:eval_exprs/7
      in call from shell:eval_loop/3
      in call from prim_file:set_cwd/2
========================================================================

Minimally, the published documentation (and the types of all functions 
of this module) has to be updated to explicitly mention this limit.

Ideally, the implementation has to change to avoid use of floats when 
manipulating Erlang integers.  IMO, it does not really have to do this 
and result in crashes like that.

Kostis



More information about the erlang-bugs mailing list