Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random number sequence to get and average

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?

like image 245
Clay Smith Avatar asked Nov 04 '22 21:11

Clay Smith


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

  1. generate random numbers [0..1(

  2. calculate their average

  3. multiply all of them to match the required average

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

like image 96
Thilo Avatar answered Nov 13 '22 21:11

Thilo