简体   繁体   English

制作宾果游戏。 如何从一组显示随机数,而不重复生成和显示哪个数?

[英]Making a BINGO game. How do I generate a random number to be displayed from one set without repeating which number is generated and displayed?

I'm making a bingo game for a course I'm taking, and the assignment is to generate a random pick/element from a pre-setup Set();, display that random value (it's going to be a letter and number eg G78, so not an integer), and then make sure that the number is not called again until the game is reset 我正在为要修读的课程制作宾果游戏,任务是从预先设置的Set()中生成随机选择/元素;显示该随机值(例如字母和数字) G78,所以不是整数),然后确保在重置游戏之前不会再次调用该号码

So far, I'm able to generate a random number and have it displayed, but I can't figure out how to keep the generator from repeating which element from the set it picks to display 到目前为止,我已经能够生成一个随机数并显示它,但是我无法弄清楚如何防止生成器重复从集合中选择显示哪个元素

let numbers = new Set();
.add("O65")
            .add("O66")
            .add("O67")
            .add("O68")
            .add("O69")
            .add("O70")
            .add("O71")
            .add("O72")
            .add("O73")
            .add("O74")
            .add("O75");

 let guess = Array.from(numbers);

 let checks = new Array();

 function getRandomNumber()
            {
                function rando()
                {
                    for(let g = guess; g = guess.length; g++)
                    {
                        let rand = guess[Math.floor(Math.random() * 
guess.length)];
                        return rand;
                    }


                }
                let num = rando();
                document.getElementById('bingo').innerHTML = num;
                checks.push(num);
                /*if(numbers.has(num))
                {
                    numbers.delete(num);
                }*/
            }

Here, I'm able to generate a random value to be displayed, but sometimes I get one that's already been called. 在这里,我可以生成一个要显示的随机值,但是有时我得到一个已经被调用的值。 I don't want that to happen, each value from the set should only be generated and displayed once until the program is reset or the entire set has been called 我不希望这种情况发生,该集合中的每个值仅应生成并显示一次,直到重置程序或调用整个集合为止

Something like this. 这样的事情。 Add every number you've chosen to a "test" list, then test to make sure the new random number isnt in the test list before returning it. 将您选择的每个数字添加到“测试”列表中,然后在返回之前进行测试以确保新的随机数不在测试列表中。 If 'rand' is in the test list, call rando again. 如果“ rand”在测试列表中,请再次致电rando。 If not, return 'rand' 如果不是,则返回“ rand”

    let test = []

     function getRandomNumber()
                {
                    function rando()
                    {
                        for(let g = guess; g = guess.length; g++)
                        {
                            let rand = guess[Math.floor(Math.random() * 
    guess.length)]}

       for(m=0; m < test.length; m++){
       if (rand == test[m]){
rando()
return false; 
}}

return rand

}

                        }


}

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

相关问题 如何从 JavaScript 中的数字数组生成非重复随机数? - How do I generate a non-repeating random from an array of number in JavaScript? 生成一个随机数而不重复先前的数字 - Generate a random number without repeating a previous number 尝试为 BINGO 游戏生成和显示非重复随机值 - Trying to generate & display non-repeating random values for a BINGO game 如何使用Google脚本设置显示在单元格中的小数位数? - How do you set the number of decimals displayed in a cell with google script? 我如何让按钮生成一个随机数,每次单击按钮时都会生成一个新数字并将其添加到前一个数字上 - how do i make a button generate a random number and each time the button is clicked a new number is generated and added to the previous number 如何在BINGO游戏中使用此功能? - How do I make this work for a BINGO game? 如何限制jQuery自动完成程序中显示的选项数量? - How do I limit the number of options displayed in jQuery's autocompleter? 如何从一个数字生成逆菱形? - How do I generate an inverse diamond from one number? 如何以随机顺序访问数组中的链接而不重复选择的最后一个链接? - How do i acces a link from array in a random order without repeating the last one chosen? 在学校并试图创造经典的猜数字游戏。 我做错了什么? - In school and trying to create the classic guess a number game. What did I do wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM