# How to make the distribution?

There is a slider from 0 to 100%
And an array of data to an indefinite number of
It is necessary to allocate 100 for all cells in the array

That is:
There is an array of dimension 5
[100, 0, 0, 0, 0] - the slider on 0%
If we specify 10%
Then 100 must be subtracted 10 and 10 be distributed across all cells other cells
[90, 8, 1, 0.7, 0.3] - something like this
April 19th 20 at 11:59
April 19th 20 at 12:01
if I understand the question

``````function randomNumbers(amount, sum) {
const rands = [...Array(amount)].map(Math.random)
const sumOfRands = rands.reduce((a, b) => a + b)
return rands.map(r => r / sumOfRands * sum) // else there is to round
}

randomNumbers(10, 100)
// [16.051734767295915, 18.859914326809577, 5.1345468410916295, 7.972670750017347, 2.1861781890798624, 17.4632115480551, 6.90229251987563, 4.367056906137147, 12.152654648732238, 8.909739502905541]

randomNumbers(4, 10)
// [2.2187088617819457, 2.872466869090836, 0.15650269298275088, 4.7523215761444675]``````

https://stackoverflow.com/a/2640067

Find more questions by tags MathematicsJavaScript