-2

I want to generate 100 numbers between 30 and 88 such that there Mean is 50 and Standard Deviation is 16. Does there exist any algorithm to solve this problem?

Rana
  • 51
  • 7
  • 1
    https://en.wikipedia.org/wiki/Normal_distribution – Robert Harvey Apr 24 '13 at 14:56
  • http://stackoverflow.com/questions/2325472/generate-random-numbers-following-a-normal-distribution-in-c-c – jbr Apr 24 '13 at 15:02
  • 1
    @RobertHarvey: a normal distribution has support on the whole real line; it sounds like the questioner wants to sample a distribution with support on an interval. – Stephen Canon Apr 24 '13 at 15:03
  • 1
    I think you should clarify the problem. Do you want the numbers to be "random"? Must the mean and standard deviation be exact? If so, there are only a small finite set of solutions, I think. – R.. GitHub STOP HELPING ICE Apr 24 '13 at 15:13
  • Yes I want numbers to be random and mean and standard deviation to be exact. – Rana Apr 24 '13 at 15:18
  • @user1437473: I suspect you have other requirements that you haven’t told us about. Would {34 repeated fifty times, 66 repeated fifty times } be an acceptable solution? It satisfies the constraints you’ve given so far. – Stephen Canon Apr 24 '13 at 15:21
  • @StephenCanon: It may be the *only* solution that satisfies the constraints, or at least one of a very small few... – R.. GitHub STOP HELPING ICE Apr 24 '13 at 15:28
  • @R.. Definitely not the only; you can construct a solution using {30,50,70}, for example, and you can mix the two. There may be a few other families as well. – Stephen Canon Apr 24 '13 at 15:49
  • The range 30..88 puts a strong constraint on the set of solutions, though. – R.. GitHub STOP HELPING ICE Apr 24 '13 at 16:02
  • @R..: There are infinitely many solutions. Any three numbers a, b, and c, where b and c equal 75 – a/2 ± sqrt(300a-a**2-5964), have mean 50 and standard deviation 16. You can pick a by any means desired and then test whether b and c are real numbers in 30 to 88. (If not, reject and try again.) Do that twice and use the six numbers to replace three 66s and three 34s in fifty pairs we started with, and you have a new solution. There are additional solutions too, that consider more than three numbers at a time. Or are we limited to integers? – Eric Postpischil Apr 24 '13 at 20:35
  • @EricPostpischil: Computers don't have real numbers. Of course integers would limit it heavily; I originally assumed integers. But even if you allow floating point, that doesn't seem to give you much more flexibility. You need 300a-a²-5964 to be a perfect square (the square of some floating point number) for your solution to work. – R.. GitHub STOP HELPING ICE Apr 24 '13 at 20:42
  • @R..: Computers have real numbers, including square roots of rational numbers. Numerical representations are not the only representations of numbers; “sqrt(3)” is a number even if you do not calculate the digits of a numeral for it in any base. – Eric Postpischil Apr 25 '13 at 01:39
  • @EricPostpischil: By real numbers, I mean the concept of working in the actual set of real numbers, which no computer can ever do. **At best**, a computer is working in the set of computable numbers, which is countable and thus covers **0%** of the real numbers. In reality, nobody works in the computable numbers. They work in fixed or arbitrary precision floating point, rationals, etc. – R.. GitHub STOP HELPING ICE Apr 25 '13 at 01:44
  • @R..: By that reasoning, mathematicians cannot work in the actual set of real numbers either. Both computers and people merely manipulate symbols. Also, it is false that a best a computer is working in the set of computable numbers. A computable number is one whose numerical value can be calculated to any desired precision. But both humans and computers can work with some numbers even if they cannot calculate them to any desired precision, by working with them symbolically. And, of course, none of that is applicable to this problem; the relations we need between the a, b, and c are computable. – Eric Postpischil Apr 25 '13 at 13:23
  • @EricPostpischil: Indeed the relations are computable, so if the problem admits solutions in a special computable number type (or even just finite extension fields of the rationals), you can get a much larger family of solutions. My objection was merely to your claim that having a huge family of real-number solutions translates automatically to a huge family of solutions on a computer, which is false in general even using the computer (or a human computer) at full strength, and fails even more quickly with real-world numerical models like floating point. – R.. GitHub STOP HELPING ICE Apr 25 '13 at 13:33

3 Answers3

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.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
-1

I found the following algorithm in this forum.

I know that it's written in Perl and that you tagged your question with C, but the algorithm is quite clear and it does exactly what you're looking for.

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

If you don't understand the code or the algorithm, please let me know. Good luck!

-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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • OP commented "I want numbers to be random and mean and standard deviation to be exact"; I guess it's not the mean of the 50 numbers should be exact, but the mean of the generating algorithm really matters. – anatolyg Apr 24 '13 at 15:27
  • generate set of numbers that match your criteria just like above in an array and then shuffle it by any random function available. bigger the array the bigger the pseudo-randomness ... but too big array can shift mean ... when you use all the numbers just shuffle it again – Spektre Oct 29 '13 at 12:05