[erlang-bugs] Bug in random seed (definition?) and random:uniform_s/2

Michael Truog mjtruog@REDACTED
Mon Mar 14 01:26:32 CET 2016


On 03/10/2016 12:59 PM, Heinz Nikolaus Gies wrote:
> This might not be critical any more since seed is deprecated but I encountered a problem with random today when passing in hand crafted seeds.
>
> The definition of a seed is: {integer(), integer(), integer()}
>
> The seed was generated by the method suggested in random:seed:
> {erlang:phash2([node()]),
>   erlang:monotonic_time(),
>   erlang:unique_integer()}
> This resulted in the seed:
>
> {128631525,-576302453291645741,-576460752303421854}
>
> That seed is valid based on the type specification of seed.
>
> Now entered random:uniform_s/2, by specification it returns a value 1 <= N <= (passed argument) BUT it doesn’t with this seed:
>
> random:uniform_s(10, {128631525,-576302453291645741,-576460752303421854}).
> {-1,{23048,-115,-29427}}
>
>
> Yes I know that  constructing and passing a seed like this is evil,  please don’t get hung on that, it is not my code it’s code I am debugging - I fully agree that it probably should be changed to rand and be done with it, still it looks is a bug.
>
> Cheers,
> Heinz
>
>
>
> _______________________________________________
> erlang-bugs mailing list
> erlang-bugs@REDACTED
> http://erlang.org/mailman/listinfo/erlang-bugs
Since the random module is deprecated in Erlang/OTP 19 and is to be removed in Erlang/OTP 20 (based on https://www.youtube.com/watch?v=YlNrWxH56_E), I took some time to modify it in ways that have been pending for a long time at https://github.com/okeuday/quickrand/blob/master/src/random_wh82.erl to force it to use positive integers for seed values (you are never suppose to allow 0 and in the past abs/1 was used in some places but not others).  The rand usage with exsplus appears to be roughly the same speed to provide a better period and no real downside, so it seems hard to argue for using the algorithm in the random module, unless something else required it.  I plan on keeping the random_wh82 module (code from the random module, modified and put into the quickrand Erlang application) in case it is needed for compatibility reasons.  So, you could avoid this problem by using quickrand or you could switch to using the newer rand module.

Best Regards,
Michael

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-bugs/attachments/20160313/43c78552/attachment.htm>


More information about the erlang-bugs mailing list