gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:57:13 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com