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]
Find more questions by tags MathematicsJavaScript