Javascript 如何在一定范围内获得一定数量的唯一随机数?

Javascript 如何在一定范围内获得一定数量的唯一随机数?,javascript,random,numbers,shuffle,Javascript,Random,Numbers,Shuffle,我有一个函数,通过用户输入告诉它要输出多少随机数,以及随机数的范围(比如1-90)。问题是它会给出重复的数字,但我只想要唯一的数字。有人知道我如何更改代码来实现这一点吗 function random() { let randomNums = []; // Get how many random numbers to output let numBox = document.querySelector('.numBox').value; // Get the highest

我有一个函数,通过用户输入告诉它要输出多少随机数,以及随机数的范围(比如1-90)。问题是它会给出重复的数字,但我只想要唯一的数字。有人知道我如何更改代码来实现这一点吗

function random() {
    let randomNums = [];

// Get how many random numbers to output
    let numBox = document.querySelector('.numBox').value;

// Get the highest number range should go to (ex. 1-70)
    let highestNumber = document.querySelector('.highestNumber').value;

// Loop to generate random numbers and push to randomNums array
    for (let i = 0; i < numBox; i++) {
        let num = Math.floor(Math.random() * highestNumber) + 1;
        randomNums.push(` ${num}`)  
    }     

// Sort numbers from lowest to highest
    randomNums.sort(function(a, b) {return a - b;});

// Output numbers
    document.querySelector('.randomOutput').innerHTML = randomNums;
}
函数随机(){
设randomNums=[];
//获取要输出的随机数
让numBox=document.querySelector('.numBox').value;
//获取最大数字范围应转到(例如1-70)
让highestNumber=document.querySelector('.highestNumber').value;
//循环生成随机数并推送到randomNums数组
for(设i=0;i
只需将循环更改为while循环,并检查数组是否还没有该值:

let i = 0;
while(i < numBox) {
    let num = Math.floor(Math.random() * highestNumber) + 1;
    if (randomNums.indexOf(num) == -1) {
        randomNums.push(num);
        i++;
    }
}
设i=0;
而(我
您只需生成并验证它是否存在,直到获得必要的随机唯一数

while (randomNums.length < numBox) {
    let num = Math.floor(Math.random() * highestNumber) + 1;
    if (randomNums.indexOf(num) === -1) randomNums.push(num);
}
while(randomNums.length
以下是一种避免
while()循环的方法

  • 用从1到最大值的数字填充数组
  • 随机化数组(我借用了Fisher-Yates算法)
  • 从数组中选取前N个项
  • 函数随机(){
    设randomNums=[];
    //获取要输出的随机数
    让numBox=document.querySelector('.numBox').value;
    //获取最大数字范围应转到(例如1-70)
    让highestNumber=document.querySelector('.highestNumber').value;
    //循环生成随机数并推送到randomNums数组
    for(设i=1;i 0;i--){
    j=数学地板(数学随机()*(i+1));
    x=a[i];
    a[i]=a[j];
    a[j]=x;
    }
    返回a;
    }
    随机()
    
    超出
    
    随机化
    如果其他人有类似问题,则这是修复该问题的代码:

    function random() {
        let randomNums = [];
        let included = {};
        let numBox = document.querySelector('.numBox').value;
        let highestNumber = document.querySelector('.highestNumber').value;
    
        for (let i =0; i<numBox; i++) {
            let temp = true;
            while(temp) {
                let num = Math.floor(Math.random() * highestNumber) + 1;
                if (included[num] === undefined) {
                    temp = false
                    randomNums.push(` ${num}`)
                    included[num] = num
                   }
            }
        }
        randomNums.sort(function(a, b) {return a - b;});
        document.querySelector('.randomOutput').innerHTML = randomNums;
    }
    
    函数随机(){
    设randomNums=[];
    让包含={};
    让numBox=document.querySelector('.numBox').value;
    让highestNumber=document.querySelector('.highestNumber').value;
    
    对于@CodeManiac提到的(设i=0;i),在@Billalsidqui解决方案中使用“Set”

    let randomNums = new Set();
    while(randomNums.size < 5) {
        let num = Math.floor(Math.random() * 10) + 1;
            randomNums.add(num);
    }
    console.log(randomNums)
    
    让randomNums=new Set();
    而(随机数小于5){
    设num=Math.floor(Math.random()*10)+1;
    randomNums.add(num);
    }
    console.log(随机数)
    
    抱歉,没有,它仍然提供重复的数字Corelected,缺少一个括号。我通过生成5个介于1和10之间的数字进行检查,仍然得到一些重复的数字HHH,推送数据“${num}”中有一个空格。现在您可以再试一次如果您使用ES7,可以使用array.includes()用于检查数组中是否存在项,而不是array.indexOf()方法。您可以跟踪以前包含的值,类似的内容可以是doneThank You@CodeManiac!这帮助我修复了它