将数字分为4个随机数 [英] Split number into 4 random numbers

查看:110
本文介绍了将数字分为4个随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将10拆分为4个随机数组成的数组,但是两个均不能为0或高于4.例如[1,2,3,4][1,4,4,1][4,2,3,1].

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], [1,4,4,1] or [4,2,3,1].

我认为这是一个简单的问题,但是由于某些原因,我无法考虑如何做到这一点.如果某人有一些说明会很有帮助!

I think it's an easy question, but for some reason I can't think of how to do this. If someone has some instruction that would be very helpful!

这是我现在拥有的代码,但是我也会生成一个总数低于10的数字:

This is the code I have now, but I generates also a total number under 10:

  let formation = [];
  let total = 0;

   for (let i = 0; i < 4; i ++) {
    if (total < 9) {
      formation[i] = Math.floor(Math.random() * 4) + 1; 
    } else {
      formation[i] = 1;
    }
  }

推荐答案

您可以创建所有可能的组合并选择一个随机数组.

You could create all possible combinations and pick a random array.

function get4() {

    function iter(temp) {
        return function (v) {
            var t = temp.concat(v);
            if (t.length === 4) {
                if (t.reduce(add) === 10) {
                    result.push(t);
                }
                return;
            }
            values.forEach(iter(t));
        };
    }
    
    const
        add = (a, b) => a + b,
        values = [1, 2, 3, 4],
        result = [];

    values.forEach(iter([]));
    return result;
}

console.log(get4().map(a => a.join(' ')));

.as-console-wrapper { max-height: 100% !important; top: 0; }

通过使用一个因子作为随机值和一个偏移量,该偏移量基于实际的总和,索引,下一个索引所需的最小和以及最大和.

It works by using a factor for the random value and an offset, based on the actual sum, index, minimum sum which is needed for the next index, and the maximum sum.

偏移量通常是最小总和,或者是总和与最大和之差的较大值.为了获得该因子,将三个值取为最小值以乘以随机值.

The offset is usually the minimum sum, or the greater value of the difference of sum and maximum sum. For getting the factor, three values are taken for the minimum for multiplying the random value.

该表根据给定值和用于获取所有值的迭代,说明了总和的所有可能值和所需的迭代.

The table illustrates all possible values of the sum and the needed iterations, based on a given value and the iteration for getting all values.

在开始时,总和是用于小部分分配的值.结果是具有14 ... 10的余数之和的第二个块,因为可以取值1 ... 5.第三轮遵循相同的规则.最后,将剩余的总和作为该值的偏移量.

At the beginning the sum is the value for distribution in small parts. The result is the second block with a rest sum of 14 ... 10, because it is possible to take a value of 1 ... 5. The third round follows the same rules. At the end, the leftover sum is taken as offset for the value.


一个具有1,...,5值和5元素且总和为15的示例以及所有可能性:


An example with 1, ..., 5 values and 5 elements with a sum of 15 and all possibilities:

min:     1
max:     5
length:  5
sum:    15

smin = (length - index - 1) * min
smax = (length - index - 1) * max
offset = Math.max(sum - smax, min)
random = 1 + Math.min(sum - offset, max - offset, sum - smin - min)

    index     sum    sum min  sum max   random   offset
  -------  -------  -------  -------  -------  -------
_      0       15        4       20        5        1
       1       14        3       15        5        1
       1       13        3       15        5        1
       1       12        3       15        5        1
       1       11        3       15        5        1
_      1       10        3       15        5        1
       2       13        2       10        3        3
       2       12        2       10        4        2
       2       11        2       10        5        1
       2       10        2       10        5        1
       2        9        2       10        5        1
       2        8        2       10        5        1
       2        7        2       10        5        1
       2        6        2       10        4        1
_      2        5        2       10        3        1
       3       10        1        5        1        5
       3        9        1        5        2        4
       3        8        1        5        3        3
       3        7        1        5        4        2
       3        6        1        5        5        1
       3        5        1        5        4        1
       3        4        1        5        3        1
       3        3        1        5        2        1
_      3        2        1        5        1        1
       4        5        0        0        1        5
       4        4        0        0        1        4
       4        3        0        0        1        3
       4        2        0        0        1        2
       4        1        0        0        1        1

该示例代码采用目标1,...,4,其长度为4个部分,总和为10.

The example code takes the target 1, ..., 4 with a length of 4 parts and a sum of 10.

function getRandom(min, max, length, sum) {
    return Array.from(
        { length },
        (_, i) => {
            var smin = (length - i - 1) * min,
                smax = (length - i - 1) * max,
                offset = Math.max(sum - smax, min),
                random = 1 + Math.min(sum - offset, max - offset, sum - smin - min),
                value = Math.floor(Math.random() * random + offset);

            sum -= value;
            return value;
        }
    );
}

console.log(Array.from({ length: 10 }, _ => getRandom(1, 4, 4, 10).join(' ')));

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于将数字分为4个随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆