Split number into 4 random numbers

前端 未结 7 840
-上瘾入骨i
-上瘾入骨i 2020-12-30 08:11

I want to split 10 into an array of 4 random numbers, but neither can be 0 or higher than 4. For example [1,2,3,4],

7条回答
  •  一生所求
    2020-12-30 08:23

    Given that:

    In a collection of n positive numbers that sum up to S, at least one of them will be less than S divided by n (S/n)

    and that you want a result set of exactly 4 numbers,

    you could use the following algorithm:

    1. Get a random number from range [1, floor(S/n)], in this case floor(10/4) = 2, so get a random number in the range of [1,2]. Lets mark it as x1.
    2. Get a random number from range [1, floor((S - x1)/(n - 1))]. Lets mark it as x2.
    3. Get a random number from range [1, floor((S - x1 - x2)/(n - 2))].
    4. Continue until you get x(n-1).
    5. Get the last number by doing S - x1 - x2 .... - x(n-1).

    Finally, extend the above algorithm with a condition to limit the upper limit of the random numbers.

    In n steps, you can get a collection.

        function getRandomInt(min, max) {
           return Math.floor(Math.random() * (max - min + 1)) + min;
        }
    
        function getRandomCollection(min, max, length, sum) {
            var collection = [];
            var leftSum = sum - (min - 1);
    
            for(var i = 0; i < length - 1; i++) {
                 var number = getRandomInt(min, Math.min(Math.ceil(leftSum/(length - i)), max));
                 leftSum -= number;
                 collection.push(number);
            }
            leftSum += min - 1;
            while(leftSum > max) {
                 var randomIndex = Math.floor(Math.random() * collection.length);
                 if(collection[randomIndex] < max) {
                      collection[randomIndex]++;
                      leftSum--;
                 }
            }
            
            collection.push(leftSum);
            return collection;
        }
        console.log(getRandomCollection(1, 4, 4, 10).join(' + ') + ' = 10');
        console.log(getRandomCollection(3, 20, 10, 100).join(' + ') + ' = 100');

    Reference

    My answer using the same algorithm for another question

提交回复
热议问题