简体   繁体   中英

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

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. If not, return '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

}

                        }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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