I know this question is difficult to understand but I need a mathematical equation for javascript.
I am having dynamic number from 0 to 7.
Now I am having 5 records:
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 possible output of a,b,c,d and e that makes average 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
User will enter desired average and i need to throw possible outputs to make filled average.
This seems to be a problem related to Integer Programming. It can be solved relatively efficiently using a Dynamic Programming strategy that maintains an invariant for smaller sub problems and merges these sub problems into a final solution. Here’s a high-level algorithm that gets you to your goal:
Example: Say i=2 and your numbers so far were 4 and 5. Then the minimally allowed third number min_3 is 6. The sum so far is 9 and 5-i = 3. 9 + 3*6 = 27 >= 25 and 9 + 3*5 = 24 < 25. This means if you chose 5 as min_3, you would no longer be able to reach your goal of a sum of 25.
Example for the entire algorithm:
Now this algorithm will produce very biased output, if you want to make it appear more randomly, simply serve a permutation of the final result, e.g. in our example you could output [5, 7, 2, 5, 6] instead.
It should be no problem to implement this in Javascript and it can be easily adapted to suit other possible ranges for x_i and the total average. Just be sure to modify the target sum as n * avg if your final sample size should be n with an average of avg.