Consider the following scenario:
min = 1
max = 5
Ave = 3
size = 5
At this stage, we have [1,?,?,?,5]
How can I determine the missing numbers? (It's simple -> [1,2,3,4,5] - but how do I create a JavaScript function to return this array)
Alternatively, given:
min = 23
max = 2500
Ave = 1007
size = 800
Now we have [23,?..,2500]
How can we calculate the missing numbers to achieve a uniform distribution?
Regarding the returned Array
- The numbers must be rounded to 2 decimal places.
- Each number in the array is greater than or equal to the preceding number.
- The plotted distribution should resemble a shallow S shaped curve.
So far, here's what has been done:
const min = 1; //lowest value
const max = 5;
const ave = 4;
const supply = 3;
const data = Array.from({length: supply}, (_, i) => i + 1);
const distArray = [];
data.map(n => {
distArray.push(Math.round((min+n*(max-min) / (supply+1)) * 1e2 ) / 1e2);
});
distArray.unshift(min);
distArray.push(max);
console.log(distArray); // returns [1,2,3,4,5]
The function above generates an array of length supply+2
between the min and max values.
The next step involves incorporating the ave
variable. What if the average of the 5 numbers was now 2.5 instead of 3? This adjustment would require a different approach... How can this be achieved?
An attempt has been made below, albeit inaccurate for larger arrays. The current formula seems to concentrate the distribution around the middle. A more effective method is needed...
const min = 5;
const max = 12;
const supply = 5;
const ave = 8;
const data = Array.from({length: supply}, (_, i) => i + 1);
const distArray = [];
const ksum = (supply + 2) * ave;
const usum = ksum - (min + max);
const parts = data.reduce((a, n) => a + n, 0);
const b = Math.floor(usum / supply);
const r = usum % supply;
data.map(n => {
distArray.push(Math.round((b + (n/parts * r)) * 1e2 ) / 1e2);
});
distArray.unshift(min);
distArray.push(max);
console.log(distArray); // returns [5, 7.27, 7.53, 7.8, 8.07, 8.33, 12]