Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi,

I need to generate 1000 random numbers with mean=250 and standard deviation=20.

I am trying to generate random numbers first and then checking for the mean and standard deviation.but i am not able to get it.

The problem i am facing is the program is giving only numbers with standard deviation near by mean.but not as required by me.(with mean=250 and standard deviation=25)

I will be very thankful if someone can help.
thanks,
babloo

  • Comment on how can i generate random numbers with predefined mean and standard deviation

Replies are listed 'Best First'.
Re: how can i generate random numbers with predefined mean and standard deviation
by Zaxo (Archbishop) on Aug 23, 2003 at 17:53 UTC

    Math::Random has exactly what you need,

    use Math::Random; my @randoms = random_normal 1000, 250, 20;
    Being random, that does not guarantee the the sample you get will have exactly that mean and sd, just close.

    If you need a distrubution other than normal, Math::Random has many to choose from.

    After Compline,
    Zaxo

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how can i generate random numbers with predefined mean and standard deviation
by simonm (Vicar) on Aug 23, 2003 at 18:15 UTC

    Laziness is a virtue.

    Before writing code, check to see if someone else has already solved this problem.

    Searching CPAN for "random AND deviation"... Result #1 is Math::Random.

    use Math::Random 'random_normal'; my @random = random_normal(1000, 250, 20)
Re: how can i generate random numbers with predefined mean and standard deviation
by Limbic~Region (Chancellor) on Aug 23, 2003 at 20:59 UTC
    babloo,
    Ok - not knowing the reason you are supposed to do this without a module I didn't spend a lot of time trying to come up with a scientific solution. I did find that the following code did work surprisingly well for the requirements you specified:
    #!/usr/bin/perl -w use strict; my $desired_mean = 250; my $desired_deviation = 20; my $total = 1000; my @numbers; gen_numbers(\@numbers, $desired_mean, $desired_deviation, $total); sub gen_numbers { my ($array_ref, $mean, $deviation, $total) = @_; my $offset = $deviation * 1.75; my $low = $mean - $offset; my $high = $mean + $offset; for (0 .. $total) { my $number; while (1) { $number = int(rand($high)) + 1; last if ($number >= $low && $number <= $high); } push @{$array_ref} , $number } } my $sum; $sum += $_ for @numbers; my $mean = $sum / $total; my $scratch; for (@numbers) { $scratch += (($_ - $mean) * ($_ - $mean)); } my $standard_deviation = sqrt($scratch / ($total - 1)); print "Desired Mean: $desired_mean\n"; print "Mean: $mean\n"; print "\n"; print "Desired Standard Deviation: $desired_deviation\n"; print "Standard Deviation: $standard_deviation\n"; __END__ Desired Mean: 250 Mean: 250.06707 Desired Standard Deviation: 20 Standard Deviation: 19.9571308028178

    I doubt seriously it will work for anything other than 20 for a standard deviation since I used the fact that 3 standard deviations from the mean accounts for 99.7% in a normal distribution. I then fudge factored the remaining .3%.

    Cheers - L~R

Re: how can i generate random numbers with predefined mean and standard deviation
by davido (Cardinal) on Aug 23, 2003 at 18:08 UTC
    There's a pretty good discussion on a close variation of that question here: 26889

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: how can i generate random numbers with predefined mean and standard deviation
by zentara (Cardinal) on Aug 24, 2003 at 19:33 UTC
    gjb wrote about the "Numerical recipes in C" book at book

    They make you download about 60 small pdfs to get the whole book. I made a simple wget script to get the whole book if you are interested. numerical-c-grabber

    It's just a brute force script, it was just too easy to cut'n'paste a lynx dump and put in a bunch of wget's. :-)