I am looking to generate a number sequence where each number is between 70 and 100 there will be x numbers in the sequence and it will give and average of y. What would this algorithm look like?
-
does the average of the numbers have to be exactly y or just when run many times come out to y on average? – hackartist Jun 22 '12 at 01:28
-
Owe, perhaps I miss understood the question :-) – 8bitwide Jun 22 '12 at 01:30
-
You won't get a good answer until you tell us the distribution. That is the hard part. – John Watts Jun 22 '12 at 01:36
-
Duplicate. https://stackoverflow.com/questions/39435481/how-to-generate-numbers-in-range-with-specific-average-with-python – Perennial Feb 16 '19 at 15:48
3 Answers
I think it is impossible for them to be uniformly distributed between 70 and 100 and have a given average at the same time.
What you can do is generate random numbers that have a given average and then scale them to fit into [70, 100] (but they will not be uniformly distributed there).
generate random numbers [0..1(
calculate their average
multiply all of them to match the required average
if any of them does not fit into [70, 100], scale all of them again by reducing their distance from
y
by the same factor (this does not change the average).x[i] = y + (x[i] - y)*scale
You will end up with numbers that are all in the range [70, 100(, but they will be uniformly distributed across a different (but overlapping) interval that is centered on y. Also, this approach only works with real/floating-point numbers. If you want integers, you got a combinational problem on your hands.

- 257,207
- 101
- 511
- 656
-
right, they can't be uniformly distributed. If y was 72 for example then the numbers between 70 and 72 must appear much more often than the numbers in 72 to 100 for them to still average to 72. – hackartist Jun 22 '12 at 01:42
-
which is still *possible* to happen in a uniform distribution, but *extremely* unlikely. Uniform distribution implies that there are *no* restrictions on how the numbers are chosen. – Thilo Jun 22 '12 at 01:43
-
by the way: do we want integers? Because if we do, this becomes a combinatory problem. My solution applies only for real numbers. – Thilo Jun 22 '12 at 01:45
Python example
import random
import time
x = 10
total = 0
avg = 0
random.seed(time.time())
for x in range(10):
total += random.randint(70,100)
avg = total /x
print "total: ", total
print "avg: ", avg

- 2,071
- 1
- 17
- 24
-
but here you want the "avg" to come out to a fixed number specified upfront. – Thilo Jun 22 '12 at 01:31
Random r = new Random();
List<int> l = new List<int>();
Console.Write("Please enter amount of randoms ");
int num = (int)Console.Read();
for (int i = 0; i < num; i++)
{
l.Add(r.Next(0, 30) + 70);
}
//calculate avg
int sum = 0;
foreach (int i in l)
{
sum += i;
}
Console.Write("The average of " + num + " random numbers is " + (sum / num));
//to stop the program from closing automatically
Console.ReadKey();

- 2,025
- 14
- 22
-
1no, part of the question was that the average must be specified before hand not found afterwards. – hackartist Jun 22 '12 at 01:40