-1

I am new to javascript and can't figure out how to solve this issue. I have a fixed value of 88.3 and I need 12 random numbers within 6.0 to 9.99 that when I sum all of them they match 88.3.

So far I managed to generate random numbers within a range using this code:

/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

 /**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Anyone could help me?

Marco Almeida
  • 1,285
  • 3
  • 17
  • 39
  • wrong method, you have to calculate your deviations from the target value – Mister Jojo Nov 12 '21 at 13:53
  • Does this answer your question? [Generate 4 random numbers that add to a certain value in Javascript](https://stackoverflow.com/questions/19277973/generate-4-random-numbers-that-add-to-a-certain-value-in-javascript) – aerial Nov 12 '21 at 14:02
  • @aerial301 thanks for the link but it does not answer. if I set the max to 88.3 for example, it returns me the first random number 75 then all of the others 1. something. That's not what I need. – Marco Almeida Nov 12 '21 at 16:01
  • @MarcoAlmeida You first have to adjust the `randombetween` function to check if the random value generated is between the desired range. If not then genereate a new one. Also, remove the `Math.floor` since you dont want integers only. – aerial Nov 12 '21 at 16:06
  • Does this answer your question? [Is there an efficient way to generate N random integers in a range that have a given sum or average?](https://stackoverflow.com/questions/61393463/is-there-an-efficient-way-to-generate-n-random-integers-in-a-range-that-have-a-g) – Peter O. Nov 12 '21 at 16:14
  • @aerial301 Now I get the random numbers but the last one always returns a negative float and out of range. – Marco Almeida Nov 12 '21 at 16:20
  • @PeterO. Thanks but the answer is way too complex and it did not resolve. – Marco Almeida Nov 12 '21 at 16:20

1 Answers1

2

Here is one way to achieve the desired result.

The original solution is this: https://stackoverflow.com/a/19278621/17175441 which I have modified to account for the numbers range limit.

Note that there are probably better ways to do this but this does the job for now:

function generate({
  targetSum,
  numberCount,
  minNum,
  maxNum
}) {
  var r = []
  var currsum = 0
  for (var i = 0; i < numberCount; i++) {
    r[i] = Math.random() * (maxNum - minNum) + minNum
    currsum += r[i]
  }

  let clamped = 0
  let currentIndex = 0
  while (currsum !== targetSum && clamped < numberCount) {
    let currNum = r[currentIndex]
    if (currNum == minNum || currNum == maxNum) {
      currentIndex++
      if (currentIndex > numberCount - 1) currentIndex = 0
      continue
    }
    currNum += (targetSum - currsum) / 3
    if (currNum <= minNum) {
      r[currentIndex] = minNum + Math.random()
      clamped++
    } else if (currNum >= maxNum) {
      r[currentIndex] = maxNum - Math.random()
      clamped++
    } else {
      r[currentIndex] = currNum
    }
    currsum = r.reduce((p, c) => p + c)
    currentIndex++
    if (currentIndex > numberCount - 1) currentIndex = 0
  }

  if (currsum !== targetSum) {
    console.log(`\nTargetSum: ${targetSum} can't be reached with the given options`)
  }
  return r
}

const numbers = generate({
  targetSum: 88.3,
  numberCount: 12,
  minNum: 6,
  maxNum: 9.99,
})
console.log('number = ', numbers)
console.log(
  'Sum = ',
  numbers.reduce((p, c) => p + c)
)
aerial
  • 1,188
  • 3
  • 7
  • 15