yuce/random

Using rand module / output range issue

jj1bdx opened this issue · 5 comments

Use :rand.uniform instead of :random.uniform (available since OTP 18).

Also, :random.uniform is not [0.0, 1.0); it's (0.0, 1.0) or 0.0 < X < 1.0 where the output is X. :rand.uniform is not either.

yuce commented

Thanks a lot for your input. When this module was written 1-2 years ago, rand module wasn't available. The doc for random:unform/0 is a bit vague, it says "Returns a random float uniformly distributed between 0.0 and 1.0" so I assumed that it meant [0.0, 1.0] but obviously I was wrong.

Your TinyMT project seems to have supported the range [0.0, 1.0) a few commits back. Is there a version of TinyMT which supports that which is also available on hex.pm (so I can add it as a dependency of this module)? Otherwise would it be OK if I copy tinymt32.erl from commit dd1f6eeb44b8fc2aea23cc1e640ea2a52534949f to this repo (with proper credits and copyright header of course)?

Use tinymt kit at hex.pm as a depencency. Simplified BSD license. I suggest you to build your own function from tinymt32:next_state/1 and tinymt32:temper/1.

For a 0.0 <= X < 1.0 range output X, use this:

temper_ge_zero_lt_one(R) ->
    tinymt32:temper(R) * (1.0 / 4294967296.0).
% works as uniform_s/1 except for the output X is 0.0 <= X < 1.0
uniform_s_ge_zero_lt_one(R0) ->
    R1 = tinymt32:next_state(R0),
    {temper_ge_zero_lt_one(R1), R1}.

Correction: tinymt-erlang is licensed by the BSD license.

yuce commented

Incorporated the code you have provided in the library. Thanks for your contribution.

👍