in reply to inclusive rand

> Naturally, the probability of getting 1 is 0 anyway, so this has little practical use, but I'm curious.

But that's exactly the point, because in practical use you want an "integer resolution". (don't know how to phrase it better, look at the examples)

This will produce all values between 0 and 10 inclusively perl -E"say int rand(11) for 1..20"

and this will produce them between 0 and 1 inclusively in 0.1 steps

C:\Windows\system32>perl -E"say 0.1 * int rand(11) for 1..20"

just improve the resolution if 0.1 is not enough, like in

C:\Windows\system32>perl -E"say 1e-6 * int rand(1e6+1) for 1..20"

edit

or with full abstraction

perl -E" $res=1e6; say 1/$res * int rand($res+1) for 1..20"

Please keep in mind that this is not cheating because in perl floats don't have anything like an infinite resolution.

That's why the probability is not 0 like you thought.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: inclusive rand
by msh210 (Monk) on Mar 13, 2017 at 12:44 UTC

    Thanks. I guess that'd work if I knew what precision rand uses.

    $_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6

      Personally I'd make sure to stay below the precision of rand, to avoid skewing the probabilities because of the multiple operations (multiplication, rounding, division) and the error they may imply. Besides, there's nothing particularily significant about the precision of rand, it's probably around the best possible precision on a float. And you could actually get a better resolution than rand with Crypt::Random.

        Thanks!

        $_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6
      You were clear and you are wrong to believe computer can produce any real numbers. (see also my update)

      We can only approximate them with floats, e.g. you will never get a the real pi, just because for instance nobody can ever finish printing a number with an infinite amount of digits.

      This means you can only operate on rational numbers with a chosen resolution.

      update

      OP changed text of Re^2: inclusive rand . I'm giving up.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Yes, but I changed it before the reply to it was posted. Sorry for the confusion.

        $_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6
      > Thanks. I guess that'd work if I knew what precision rand uses.

      you can't go to full precision.

      Please learn the basics of floating numbers and stop randomly changing your posts.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        If I can't go to full precision, then I don't see how the solution you (and another) proposed, subdividing the interval from 0 to a large integer, is the same as inclusive rand. It will always be less precise, i.e. choosing among fewer numbers. No?

        $_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6