Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I want if() to be true at a rate of 1/40.000, however, rand() is not precise enough and outputs 0 quite often

That's not the way random works. It doesn't guarantee 1/40000, if it did, it would not be "random".

It only guarantees that the ratio will tend towards 1/40000 over time.

Most times it will produce 1/40000; but sometimes it will be 0/40000; sometimes 2/40000; occasionally 3/40000; very occasionally even 4/40000; but over time they will tend to balance each other out and the ratio will tend to get closer and closer to 1/40000.

Try running this for a while to see how it works:

#! perl -slw use strict; use Math::Random::MT qw[ rand ]; use constant LIMIT => 1 / 40000; $|++; my( $aveTrue, $aveFalse ) = (0,0); while( 1 ) { my( $true, $false ) = (0,0); for( 1 .. 40000 ) { if( rand() < LIMIT ) { ++$true; } else { ++$false; } } $aveTrue += $true; $aveFalse += $false; printf "\rThis time: $true/$false; Accumulate ratio: %f / 40000", +$aveTrue / ( $aveFalse / 40000 ); } __END__ C:\test> This time: 0/40000; Accumulate ratio: 0.995570 / 40000

If you need to always get one true from every 40,000, then use:

use List::Util qw[ shuffle ]; my @numbs = shuffle 1 .. 40000; for my $num ( @numbs ) { if( $num == 1 ) { ... } }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

In reply to Re: rand() precision low, looking for a fast way to get high precision rand float? by BrowserUk
in thread rand() precision low, looking for a fast way to get high precision rand float? by Klaas

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2018-06-24 05:53 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.