This should be really simple, but it's driving me nuts. The code below should calculate how many errors you would get per million iterations at a given rate of error. It does the calculation 5 times, just to demonstrate the distribution of errors.
for ($x=10; $x<1000000; $x=$x*10){ $error_rate = $x; print "Error rate = 1 every $x, errors = "; for ($y=0;$y<5;$y++){ errortest(); } print "\n"; } sub errortest { $errors = 0; for ($n=0;$n<1000000;$n++){ $random = int (rand($error_rate)); if ($random == 1){$errors++} } print $errors, " "; } Output: Error rate = 1 every 10, errors = 99778 100371 99586 99912 99778 Error rate = 1 every 100, errors = 10007 9962 9803 10063 10132 Error rate = 1 every 1000, errors = 1004 1019 1049 1011 945 Error rate = 1 every 10000, errors = 90 84 87 79 83 Error rate = 1 every 100000, errors = 0 0 0 0 0
My question: why aren't the values correct? You'd expect 100 errors at a 1 in 10000 error rate, not the 84 (average) above. At a 1 in 100000 rate you'd expect 10, not 0. What's going on here - I'm going crazy. This should be so simple ...

In reply to Math all gone wrong... by carlos fandango

Title:
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, details, 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, summary, 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.