Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 随机数生成算法_Node.js_Algorithm - Fatal编程技术网

Node.js 随机数生成算法

Node.js 随机数生成算法,node.js,algorithm,Node.js,Algorithm,我不知道应该使用哪种算法。如有必要,平台为Node.js Volume = 100; Range = 5...15; N = 10. 我需要从一个范围中生成N数字,这个范围的总和将给出数量。最好的方法是什么?下面是一个简单的快速实现 也许有一个数字技巧可以使这一过程更快,但这是可行的 请注意,当然可以传入使此函数永远无法完成的参数组合,但对于total=100,min=5,max=15,n=10,它确实找到了解决方案,例如 [ 14, 5, 11, 6, 10, 13, 9, 13, 14,

我不知道应该使用哪种算法。如有必要,平台为
Node.js

Volume = 100; Range = 5...15; N = 10.

我需要从一个范围中生成
N
数字,这个范围的总和将给出数量。最好的方法是什么?

下面是一个简单的快速实现

也许有一个数字技巧可以使这一过程更快,但这是可行的

请注意,当然可以传入使此函数永远无法完成的参数组合,但对于
total=100,min=5,max=15,n=10,它确实找到了解决方案,例如

[ 14, 5, 11, 6, 10, 13, 9, 13, 14, 5 ]
[ 15, 13, 14, 10, 9, 8, 10, 8, 6, 7 ]
[ 8, 11, 11, 9, 12, 15, 5, 9, 6, 14 ]
[ 9, 12, 11, 6, 14, 12, 10, 7, 11, 8 ]
[ 9, 8, 9, 10, 11, 10, 12, 7, 14, 10 ]
[ 9, 9, 5, 14, 10, 13, 11, 13, 9, 7 ]

函数生成器枚举数(总计、最小值、最大值、n){
while(true){
常量数=[];
设和=0;
while(true){
让num;
如果(numbers.length==n-1){
//用余数填入最后一个数字以满足问题
num=总和;
如果(nummax){
//超出范围,此解决方案无效
break;//再试一次。
}
}否则{
num=Math.floor(min+Math.random()*(max+1-min));
}
push(num);
sum+=num;
如果(numbers.length==n&&sum==total){
//找到解决方案。
返回号码;
}
如果(总和>总数){
//打捞,重试。
打破
}
}
}
}
日志(generateNumbers(100,5,15,10));

你需要
范围中可能的
数字
?有无重复?@Dave没关系。是的,我脑子里只有这个想法。我担心会有大量的时间,这可能会用于生成。当然,在这个例子中不是这样。不过,谢谢你的帮助!
function generateNumbers(total, min, max, n) {
  while (true) {
    const numbers = [];
    let sum = 0;
    while (true) {
      let num;
      if (numbers.length === n - 1) {
        // Fill in the last number with the remainder to satisfy the problem
        num = total - sum;
        if (num < min || num === 0 || num > max) {
          // Out of bounds, this solution is invalid
          break; // Try again.
        }
      } else {
        num = Math.floor(min + Math.random() * (max + 1 - min));
      }
      numbers.push(num);
      sum += num;
      if (numbers.length === n && sum === total) {
        // Solution found.
        return numbers;
      }
      if (sum > total) {
        // Overshot, retry.
        break;
      }
    }
  }
}

console.log(generateNumbers(100, 5, 15, 10));