JavaScript 检查是否尚未使用随机数

标签 javascript arrays iteration

我需要获取随机数,但不能多次使用相同的数字。 我编写了以下函数来创建一个随机数并检查它是否尚未被使用。

function randomRecep(){
 return Math.floor((Math.random() * 3));
}

function assignTemp(tempArr){
  console.log("Started assign function, tempArr has: "+tempArr);
  var num = randomRecep();
  for (var i = 0; i < tempArr.length; i++) {
    console.log("num is: " + num + ". check for doubles. tampArr["+i+"] is: "+tempArr[i]);
    if (num == tempArr[i]){
        console.log("FOUND DOUBLE! random num = "+num+" IS ALREADY IN array:"+tempArr)
        assignTemp(tempArr);
        break;
    }
  }    
  tempArr.push(num);
  console.log("pushed " + num + "into array. now is:" + tempArr);
  return num;
}

以下是控制台输出。似乎检查有效但由于某种原因在检查结束时而不是仅仅推送唯一随机数并返回其值,似乎程序还推送所有先前的重复数字并返回第一个随机数而不是最后一个通过检查的。这是为什么?

Started assign function, tempArr has: -1,2,1
code.js:104 num is: 1. check for doubles. tampArr[0] is: -1
code.js:104 num is: 1. check for doubles. tampArr[1] is: 2
code.js:104 num is: 1. check for doubles. tampArr[2] is: 1
code.js:106 FOUND DOUBLE! random num = 1 IS ALREADY IN array:-1,2,1
code.js:101 Started assign function, tempArr has: -1,2,1
code.js:104 num is: 1. check for doubles. tampArr[0] is: -1
code.js:104 num is: 1. check for doubles. tampArr[1] is: 2
code.js:104 num is: 1. check for doubles. tampArr[2] is: 1
code.js:106 FOUND DOUBLE! random num = 1 IS ALREADY IN array:-1,2,1
code.js:101 Started assign function, tempArr has: -1,2,1
code.js:104 num is: 0. check for doubles. tampArr[0] is: -1
code.js:104 num is: 0. check for doubles. tampArr[1] is: 2
code.js:104 num is: 0. check for doubles. tampArr[2] is: 1
code.js:113 pushed 0into array. now is:-1,2,1,0

这个结果很好,我们的想法是到此为止。但该过程继续进行:

code.js:113 pushed 1into array. now is:-1,2,1,0,1
code.js:113 pushed 1into array. now is:-1,2,1,0,1,1

我找到了实现上述目标的更简单的代码。但是,我正在努力学习,但我还不明白我的方法最后出了什么问题。逻辑上的漏洞在哪里?

最佳答案

所以你当前代码的问题是你使用了break,这里应该使用return

if (num == tempArr[i]){
    console.log("FOUND DOUBLE! random num = "+num+" IS ALREADY IN array:"+tempArr)
    assignTemp(tempArr);
    break; // <-- should be return instead
}

这样做的原因是,一旦找到一个非唯一数字,您将重新开始搜索下一个数字,但是 break 将退出循环,并在 for 循环之后直接添加 num 到你的阵列。因此,它将首先添加一个可能新的唯一编号,然后退出该循环,然后返回以退出您的第一个循环,然后添加非唯一编号;)

你也可以按照下面的方式重写你的代码(不知道你对某些JavaScript版本有什么要求,或者你只允许使用for循环)

function randomRecep(){
 return Math.floor((Math.random() * 3));
}

function assignTemp(tempArr){
  const number = randomRecep();
  if (tempArr.includes( number ) ) {
    console.warn( `${number} exists in [${tempArr.join(', ')}]` );
    return assignTemp(tempArr);
  }
  console.warn( `adding ${number} to [${tempArr.join(', ')}]` );
  tempArr.push( number );
  return tempArr;
}

const output = [];
// shouldn't call this more than the nr of output possibilities (and the pool here has 3 options)
assignTemp( output );
assignTemp( output );
assignTemp( output );
// we will always expect 0, 1, 2 in the output in some manner
console.log( output );

关于JavaScript 检查是否尚未使用随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53842317/

相关文章:

emacs - `let loop`(名为let)emacs lisp中的替代方案

javascript - Jquery - 动态添加的输入字段不会发送值以发布

javascript - Puppeteer 未拾取对话框

javascript - 响应式导航在移动设备上的表现不尽如人意

c - 如何通过更改符号以保留方式将数组复制到另一个数组

sql - 在 Postgres 中搜索整数数组

python - 查找大量键 : dictionary vs. NumPy 数组

jasper-reports - 碧 Jade 报告 : iterating List inside JRXML

javascript - 在javascript中使用来自html框的输入

c++ - 我们如何让我们的 C++ 程序自行修复数组索引超出范围?