Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: inclusive rand

by msh210 (Monk)
on Mar 13, 2017 at 12:44 UTC ( [id://1184404]=note: print w/replies, xml ) Need Help??


in reply to Re: inclusive rand
in thread inclusive rand

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

Replies are listed 'Best First'.
Re^3: inclusive rand
by Eily (Monsignor) on Mar 13, 2017 at 13:07 UTC

    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
Re^3: inclusive rand
by LanX (Saint) on Mar 13, 2017 at 12:52 UTC
    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
Re^3: inclusive rand
by LanX (Saint) on Mar 13, 2017 at 13:02 UTC
    > 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
        in reality one wants random results which are equally distributed.

        This consequently means one decides how big the result set is in order to define the requested probability of one single element of this set (which is actually only approximated) and some other criteria like distribution (also approximated)

        If ... for any applications which are beyond my experience ... the resolution of the build in rand is not enough, you'd simply use another rand-module from CPAN in conjunction with something like bignum

        Now ... asking for equally distributed real numbers is nonsensical, because you can't tell the expected probability of say pi.

        And even if you used a natural random number generator ... like by measuring radio activity ... you'd finally be confronted with the limits of quantum mechanics. There is no unlimited precision.

        We live in reality, and computers are build into the real world (i.e. this universe)

        This means a random number is always ultimately some integer in disguise.

        Sorry your question can't be answered otherwise because it would become unreal.

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

        > It will always be less precise, i.e. choosing among fewer numbers. No?

        even if you don't want to use a self defined precission you could try this

        perl -Mposix -E" say rand(1+1/2**47) for 1..20"

        So why 47?

        Just try and error on my system

        C:\Windows\system32>perl -E" say 1+1/2**47;" + 1.00000000000001 C:\Windows\system32>perl -E" say 1+1/2**48;" 1

        I'm too lazy to figure out how exactly the limits of the mantissa is used internally and where the caveats are.

        And taking the rounding errors in these into assumption this shouldn't matter much. (as BUK mentioned under windows before 5.20 rand is limted to 15 bits only.)

        The practical value of this is too small to justify more elaboration.

        update

        or alternatively to avoid floats and rounding errors

        C:\Windows\system32>perl -E"$r=int rand(1e15+1); say $r==1e15 ? 1 : sp +rintf '0.%015s', $r" 0.176483154296875

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found