-2

我想生成 30 到 88 之间的 100 个数字,使得平均值为 50,标准偏差为 16。是否存在任何算法来解决这个问题?

4

3 回答 3

0

I assume that you don't really want to generate 100 numbers; i assume you want an algorithm that generates random numbers between the given bounds, with a given mean and standard deviation.

You can try generating numbers that have normal distribution (suggested by @Robert Harvey), and discard those that are outside your range. In pseudo-code:

do {
    x = Generate_Random_Number_Normal_Distribution(mean = 50, STD = y);
} while (!(x >= 30 && x <= 88));

If y = 16, the result will have slightly smaller standard deviation than what you wanted. In order to get the standard deviation you want, you have to increase the y parameter.

But by how much to increase? You can decide this by trial and error: testing different values of y, generating e.g. 1000000 numbers, calculating their standard deviation, and looking whether it's close enough to what you want.

After you find out what y is, make it a constant, and you will get an algorithm for generating your 50 numbers.

于 2013-04-24T15:40:48.937 回答
-1

我在这个论坛中找到了以下算法。

我知道它是用 Perl 编写的,并且您用 C 标记了您的问题,但是该算法非常清晰,并且完全符合您的要求。

#!/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

如果您不理解代码或算法,请告诉我。祝你好运!

于 2013-04-24T15:04:33.557 回答
-1

The 100 numbers 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, and 34 are between 30 and 88 and have a mean of 50 and a standard deviation of 16.

The method of generating them is obvious. I suspect your actual problem has other criteria that you have not stated.

于 2013-04-24T15:23:37.430 回答