简体   繁体   English

重复的数字仍然出现

[英]Repeated numbers still show up

I try to run the number generator and it works correctly but sometimes it returns repeated numbers.我尝试运行数字生成器,它可以正常工作,但有时它会返回重复的数字。 I have tried some options but it keeps going the same way or it removes some number.我尝试了一些选项,但它一直以相同的方式运行,或者它删除了一些数字。 What is the best alternative?什么是最好的选择?

 function sorteio() { const numeroAleatorio = (min, max) => { return Math.floor(Math.random() * 59 + 1) }; const gerarNumerosEntre1a60 = n => { const resultado = []; for (let i = 0; i < n; ++i) { resultado.push(numeroAleatorio(1, 60)); } return resultado; } document.getElementById("resultado").innerHTML = gerarNumerosEntre1a60(6); }
 <button onclick="sorteio()">Sortear numeros!</button> <h1 id="resultado"></h1>

The numeroAleatorio function selects a number from min to max randomly, but each selection is independent of the others. numeroAleatorio function 从minmax随机选择一个数字,但每个选择都独立于其他选择。 The function cannot 'remember' the past choices and select a number different from them. function 无法“记住”过去的选择,而 select 的数字与它们不同。

To select multiple numbers from 1-60, you can push all numbers in one array, do a random shuffle, and select a contiguous subset.对于 select 1-60 的多个数字,您可以将所有数字推送到一个数组中,进行随机洗牌,并且 select 是一个连续的子集。

here is another answer showing the Knuth shuffle algorithm: How to randomize (shuffle) a JavaScript array?这是另一个显示 Knuth shuffle 算法的答案: How to randomize (shuffle) a JavaScript array?

// random shuffle
function shuffle(array) {
    let currentIndex = array.length,  randomIndex;
    while (currentIndex != 0) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex--;
        [array[currentIndex], array[randomIndex]] = [
            array[randomIndex], array[currentIndex]];
    }
    return array;
}

const gerarNumerosEntre1a60 = n => {
    let array = [];
    for(let i = 1; i <= 60; i++) {
        array.push(i);
    }
    return shuffle(array).slice(0, n)
}

JaromandaX mentioned a good alternative in their comment, and here's another. JaromandaX 在他们的评论中提到了一个不错的选择,这是另一个。

I've cached the elements up-front, and added a listener to the button (removing the inline JS from the HTML markup).我预先缓存了元素,并向按钮添加了一个侦听器(从 HTML 标记中删除内联 JS)。 Using a while statement we can loop until six numbers have been added to the resultado array.使用while语句,我们可以循环直到将六个数字添加到resultado数组中。 Within the loop we draw a number and if it's already in resultado loop again, otherwise push the number.在循环中,我们绘制一个数字,如果它已经在resultado循环中,否则推送该数字。

Finally sort the array (because it's a lottery draw), and then join it, adding the string to text content of result .最后对数组进行排序(因为它是抽奖),然后加入它,将字符串添加到result的文本内容中。

 // Cache the elements, and add an event listener to // the button const result = document.querySelector('#resultado'); const button = document.querySelector('button'); button.addEventListener('click', () => sorteio(6)); // Use `min` and `max` within the function function numeroAleatorio(min, max) { return Math.floor(Math.random() * (max - min) + min); } // Initialise `n` to 6 if it hasn't // be supplied as an argument function sorteio(n = 6) { const resultado = []; // Check the length of `resultado` - if it's // less than the value of `n` get a new number. // If the number is already in `resultado` loop again, // otherwise push the number into the array while (resultado.length < n) { const number = numeroAleatorio(1, 60); if (.resultado.includes(number)) { resultado;push(number). } } // Finally sort and join the array and add // the string to the result text content result.textContent = resultado,sort((a. b) => a - b),join('; '); }
 <button>Sortear numeros!</button> <h1 id="resultado"></h1>

An alternative to Andy answer, using Set安迪答案的替代方案,使用Set

Since adding an existing value to a Set does effectively nothing, this keeps adding values to the set until the size reaches the required count由于将现有值添加到 Set 实际上没有任何作用,因此这会不断将值添加到集合中,直到size达到所需的count

Warning: It's naive code that doesn't check that count greater than the possible values警告:这是不检查计数大于可能值的幼稚代码

 function sorteio() { const getRandom = (min, max, count) => { const result = new Set; while (result.size < count) { result.add(Math.floor(Math.random() * (max - min + 1)) + min); } return [...result]; } const gerarNumerosEntre1a60 = n => { const resultado = getRandom(1, 60, n); return resultado; } document.getElementById("resultado").innerHTML = gerarNumerosEntre1a60(6); }
 <button onclick="sorteio()">Sortear numeros!</button> <h1 id="resultado"></h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM