0

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 e.g. 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

p4yner56
  • 17
  • 1
  • 1
  • 7
  • I'd use an array, randomly shuffled (sorted), and then s(p)lice however many values you want from it – Jaromanda X Aug 27 '19 at 22:57
  • Do exactly what a Bingo machine does: put one of each ball into a container, and randomly remove one at a time. Put the numbers 1..75 in a list, shuffle the list, and then pop one number at a time off the list. – Lee Daniel Crocker Aug 27 '19 at 23:52

1 Answers1

0

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

}

                        }


}
Jay
  • 1,289
  • 3
  • 11
  • 22