Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Perl rand() generates larger numbers for small sample size, bug!

by Bethany (Scribe)
on Aug 08, 2014 at 01:12 UTC ( [id://1096699]=note: print w/replies, xml ) Need Help??


in reply to Perl rand() generates larger numbers for small sample size, bug!

You aren't clearing $sum at the beginning of each outer loop. Try this:

#!/usr/bin/perl use strict; use warnings; my $sampleSize = 7; my ($sum, $ssum); for my $i (1..1000){ $sum = 0; for my $j (1..$sampleSize){ $sum+=rand(); } $sum/=$sampleSize; $ssum+=$sum; } $ssum/=1000; print "$ssum\n";

If you think you've found a show-stopping bug in a core function that others have been using successfully for years or decades, it's a good idea to examine your own code before declaring it a bug in Perl.

Edited to add: Besides, how could the rand() function "know" how many times you're about to call it, or how many you're not going to call it? Short of a maliciously buggy dev-teasing version of Perl, which is too far-fetched to consider, there's no possible mechanism that could do this.

  • Comment on Re: Perl rand() generates larger numbers for small sample size, bug!
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl rand() generates larger numbers for small sample size, bug!
by choroba (Cardinal) on Aug 08, 2014 at 06:30 UTC
    Great explanation! Just a nitpick: If you don't need the value of $sum outside the loop, declare it inside:
    my $ssum = 0; # $sum not needed here for my $i (1..1000){ my $sum = 0; # but here. for my $j (1..$sampleSize){ $sum+=rand(); } $sum/=$sampleSize; $ssum+=$sum; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Perl rand() generates larger numbers for small sample size, bug!
by Laurent_R (Canon) on Aug 08, 2014 at 06:25 UTC

    If you think you've found a show-stopping bug in a core function that others have been using successfully for years or decades, it's a good idea to examine your own code before declaring it a bug in Perl.

    My understanding is not that the OP claimed to have found a bug in rand, but asked a question because his or her tests did not behave as expected. OK, the test was buggy, and it is very good that you pointed out where the mistake is, but no reason to be harsh on the OP. After all, we all make mistakes (well, at least I do), actually, making mistakes is a great way to learn how not to make them again.
      My understanding is not that the OP claimed to have found a bug in rand

      The subject of the thread is "Perl rand() generates larger numbers for small sample size, bug!"

      Anyway, mistakes are fine, but if one finds oneself saying "I found a bug" but also "I am not an expert", perhaps more cautious wording or some double-checking before crying wolf is in order :-)

      You're right, of course. I was trying to be constructive rather than harsh. In fact, one of my previous replies contained the very same advice you give in your last sentence.

      If my reply came off as a flame, however mild and non-scorchy, Ravi and you both have my apologies.

Re^2: Perl rand() generates larger numbers for small sample size, bug!
by ravipatel4 (Novice) on Aug 08, 2014 at 03:00 UTC
    Okay now this is embarrassing. I should have looked at my code carefully. I am sorry for that. Is there a way to delete this post and prevent it to become more embarrassing for me?

    Thank you for your time.

      No need to delete it, and I believe Perl Monks' policy is to discourage deleting posts. Everyone makes mistakes. If we learn from them, we're wiser for having made them.

      Deleting nodes is strongly discouraged. However, if you like, you can edit your post, using <strike> tags and marking updates as such to correct it - see "How do I change/delete my post?" for more good information on this. You could explain what the problem was and how it was fixed, that would maximize what others can learn from your post.

      Mistakes happen, don't worry about it! Acknowledging them and fixing them is much more honorable and productive than trying to hide or defend them :-)

        Exactly ... and, just look at all the very productive comments which followed this point in the post-thread! You aren't the first and you won't be the last intrepid soul to have done this, questioned this, asked this. That's why PM is here.
        Thank you for you suggestion. I have strike through my question, so that it doesn't mislead others. Thank you for your time.

        Best,

        Ravi

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found