Re: Bug in random seed (definition?) and random:uniform_s/2

Michael Truog <[email protected]> Sun, 13 Mar 2016 17:26:32 -0700
Newsgroups gmane.comp.lang.erlang.bugs
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============2264525768364131465==
Content-Type: multipart/alternative;
 boundary="------------050106010206080002070204"

This is a multi-part message in MIME format.
--------------050106010206080002070204
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: quoted-printable

On 03/10/2016 12:59 PM, Heinz Nikolaus Gies wrote:
> This might not be critical any more since seed is deprecated but I enco=
untered 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 <=
=3D N <=3D (passed argument) BUT it doesn=92t 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,  pl=
ease don=92t get hung on that, it is not my code it=92s code I am debuggi=
ng - 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
> [email protected]
> http://erlang.org/mailman/listinfo/erlang-bugs
Since the random module is deprecated in Erlang/OTP 19 and is to be remov=
ed in Erlang/OTP 20 (based on https://www.youtube.com/watch?v=3DYlNrWxH56=
_E), I took some time to modify it in ways that have been pending for a l=
ong time at https://github.com/okeuday/quickrand/blob/master/src/random_w=
h82.erl to force it to use positive integers for seed values (you are nev=
er suppose to allow 0 and in the past abs/1 was used in some places but n=
ot others).  The rand usage with exsplus appears to be roughly the same s=
peed 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 els=
e required it.  I plan on keeping the random_wh82 module (code from the r=
andom module, modified and put into the quickrand Erlang application) in =
case it is needed for compatibility reasons.  So, you could avoid this pr=
oblem by using quickrand or you could switch to using the newer rand modu=
le.

Best Regards,
Michael


--------------050106010206080002070204
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta content=3D"text/html; charset=3Dwindows-1252"
      http-equiv=3D"Content-Type">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">On 03/10/2016 12:59 PM, Heinz Nikolaus
      Gies wrote:<br>
    </div>
    <blockquote
      cite=3D"mid:[email protected]"
      type=3D"cite">
      <meta http-equiv=3D"Content-Type" content=3D"text/html;
        charset=3Dwindows-1252">
      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.
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">The definition of a seed is:=A0<span
          style=3D"font-family: Courier, monospace; font-size: 13px;
          font-weight: bold; background-color: rgb(255, 255, 255);"
          class=3D"">{integer(), integer(), integer()}</span></div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">The seed was generated by the method suggested in
        random:seed:</div>
      <div class=3D"">
        <pre style=3D"font-family: Courier, monospace; font-size: 13px; b=
ackground-color: rgb(238, 238, 255);" class=3D"">{erlang:phash2([node()])=
,
 erlang:monotonic_time(),
 erlang:unique_integer()}</pre>
        <div class=3D"">This resulted in the seed:</div>
      </div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">{128631525,-576302453291645741,-576460752303421854}=
</div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">That seed is valid based on the type specification
        of seed.</div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">Now entered random:uniform_s/2, by specification it
        returns a value 1 &lt;=3D N &lt;=3D (passed argument) BUT it does=
n=92t
        with this seed:</div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">
        <div class=3D"">random:uniform_s(10,
          {128631525,-576302453291645741,-576460752303421854}).</div>
        <div class=3D"">{-1,{23048,-115,-29427}}</div>
      </div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">Yes I know that =A0constructing and passing a seed
        like this is evil, =A0please don=92t get hung on that, it is not =
my
        code it=92s 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.</div>
      <div class=3D""><br class=3D"">
      </div>
      <div class=3D"">Cheers,</div>
      <div class=3D"">Heinz</div>
      <div class=3D""><br class=3D"">
      </div>
      <br>
      <fieldset class=3D"mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap=3D"">_______________________________________________
erlang-bugs mailing list
<a class=3D"moz-txt-link-abbreviated" href=3D"mailto:[email protected]=
rg">[email protected]</a>
<a class=3D"moz-txt-link-freetext" href=3D"http://erlang.org/mailman/list=
info/erlang-bugs">http://erlang.org/mailman/listinfo/erlang-bugs</a>
</pre>
    </blockquote>
    <tt>Since the random module is deprecated in Erlang/OTP 19 and is to
      be removed in Erlang/OTP 20 (based on
      <a class=3D"moz-txt-link-freetext" href=3D"https://www.youtube.com/=
watch?v=3DYlNrWxH56_E">https://www.youtube.com/watch?v=3DYlNrWxH56_E</a>)=
, I took some time to
      modify it in ways that have been pending for a long time at </tt><a=
 class=3D"moz-txt-link-freetext" href=3D"https://github.com/okeuday/quick=
rand/blob/master/src/random_wh82.erl">https://github.com/okeuday/quickran=
d/blob/master/src/random_wh82.erl</a>
    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).=A0 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.=A0 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.=A0 So, you could avoid this problem by using
    quickrand or you could switch to using the newer rand module.<br>
    <br>
    Best Regards,<br>
    Michael<br>
    <br>
  </body>
</html>

--------------050106010206080002070204--

--===============2264525768364131465==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
erlang-bugs mailing list
[email protected]
http://erlang.org/mailman/listinfo/erlang-bugs

--===============2264525768364131465==--