How do you get the possible results of getting a fixed average?
I know this question is difficult to understand, but I need a math equation for javascript.
I am having dynamic number from 0 to 7.
Now I have 5 entries:
a = from 0 to 7,
b = from 0 to 7,
c = from 0 to 7,
d = from 0 to 7,
e = from 0 to 7
Now I need an equation from which I can find the possible output of a, b, c, d and e, which is the average of 5.
Like for average = 5:
1. a = 5, b = 5, c = 5, d = 5, e = 5
2. a = 2, b = 7, c = 6, d = 5, e = 5
The user enters the desired average and I need to reset the possible outputs to make the filled average.
source to share
It seems to be a problem with integer programming. It can be solved efficiently with a dynamic programming strategy that maintains an invariant for small sub-processes and combines these sub-routines into a final solution. Here's a high-level algorithm to help you reach your goal:
1) Start with a random number of seeds, x_1 between 0..7
2) Now do the following three times (i = 2, 3, 4):
3) Find the smallest possible next number. To average 5 for five numbers, your target total for all five is 25. So you need to make sure that you don't deny yourself the ability to reach 25 points. So let's say min_i + 1 defines the smallest number 0..7, so the sum of all x_i so far plus min_i + 1 * (5-i)> = 25.
Example : say i = 2, and your numbers are still 4 and 5. Then the minimum allowable third number min_3 is 6. The sum is still 9 and 5-i = 3. 9 + 3 * 6 = 27> = 25 and 9 + 3 * 5 = 24 <25. This means that if you chose 5 as min_3, you will no longer be able to reach your goal in the amount of 25.
4) Choose x_i + 1 as a random number between min_i + 1 and 7.
5) Finally, select x_5 as 25 - the sum of x_1..x_5. Output x_1 - x_5.
An example for the whole algorithm:
Let x_1 = 6.
=> min_2 = 5, sum = 6
Let x_2 = 5.
=> min_3 = 5, sum = 11
Let x_3 = 7.
=> min_4 = 4, sum = 18
Let x_4 = 5
=> sum = 23
=> x_5 = 2 (= 25 - 23)
Output: [6, 5, 7, 5, 2], which really has an average of 5.
Now this algorithm will produce very biased output, if you want to make it more random just do a rearrangement of the final result eg. in our example, you could output [5, 7, 2, 5, 6].
Javascript shouldn't have any implementation issues and can be easily adapted for other possible ranges for x_i and overall mean. Just remember to change the target sum to be n * avg if your final sample size should be n with an average avg.
source to share