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

Kostis Sagonas <>
Tue Feb 11 15:47:25 CET 2014

The documentation of random:uniform/1 reads:

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


     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).
2> random:uniform(1 bsl 1023).
3> random:uniform(1 bsl 1024).
** exception error: an error occurred when evaluating an arithmetic 
      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.


More information about the erlang-bugs mailing list