[erlang-bugs] Bug in module random (stdlib-1.16.5, R13B04)

Florian Schintke <>
Mon Jun 14 23:15:48 CEST 2010


[Raimo Niskanen]
> Thank you for reporting this problem. Your suggested fix to replace
> 0 with 1 in the seed also seems as the least bad workaround.
> 
> I am amazed nobody reported this before.
> 
> It will be fixed or at least documented in earliest feasible release
> (unfortunately not R14A).

To also fix the additional problem that a 0 could be generated by the
modulo operation when numbers for a seed are larger than the prime
numbers used in the algorithm, one should do first the calculation as
currently done and then check whether the result is zero.  So my
proposal for a new random:seed/3 would be something similar to this:

seed(A1, A2, A3) ->
    case abs(A1) rem 30269 of 0 -> T1 = 1; T1 -> T1 end,
    case abs(A2) rem 30307 of 0 -> T2 = 1; T2 -> T2 end,
    case abs(A3) rem 30323 of 0 -> T3 = 1; T3 -> T3 end,
    put(random_seed, {T1, T2, T3}).


Florian
-- 
Florian Schintke <>, http://www.zib.de/schintke/


More information about the erlang-bugs mailing list