I am working on making a bingo game. I've gotten as far as being able to get a random number generated and displayed at the click of a button. My only issue is that some values will end up being generated more than once. THAT IS THE PART I'M POSTING THIS QUESTION FOR. EVERY VALUE SHOULD ONLY BE GENERATED & DISPLAYED ONCE UNTIL THE GAME IS RESET. Does anybody have any example code, preferably for how to use something like the splice() method that I keep hearing brought up?
I CAN ALREADY GENERATE THE RANDOM NUMBER FROM THE SET AND DISPLAY IT. I'M ONLY LOOKING TO MAKE SURE THAT NUMBERS THAT ARE GENERATED ARE NOT REPEATED.
<head>
<title>BINGO</title>
</head>
<body>
<div id="bingo">
<script>
let numbers = new Set()
.add("B1")
.add("B2")
.add("B3")
.add("B4")
.add("B5")
.add("B6")
.add("B7")
.add("B8")
.add("B9")
.add("B10");
let called = Array.from(numbers);
let display = new Array();
function getRandomNum()
{
function rando()
{
for (let i = called.length - 1; i > 0; i++)
{
const j = Math.floor(Math.random() * called.length);
const number = called[i];
called[i] = called[j];
called[j] = number;
return number;
//let show = called[Math.floor(Math.random() * called.length)];
//return show;
}
//document.getElementById('bingo').innerHTML = display[0];
}
let index = rando();
document.getElementById('bingo').innerHTML = index;
display.push(index);
}
function show()
{
for(let n = 0; n < display.length; n++)
{
document.getElementById('reveal').innerHTML += "<br/>" + display[n] + "<br/>";
}
}
</script>
</div>
<div id="button">
<button onclick="getRandomNum()">Random Number</button>
</div>
<br/>
<br/>
<br/>
<div id="reveal">
<button onclick="show()">Numbers Called</button>
</div>
</body>
</html>
Looking for some help to prevent number generated from being repeated (EXAMPLE CODE PREFERRED)
You could create an array in which you store all numbers which have already been selected. Then, when randomly selecting a new number, continue to randomize until a number has been selected which is not within that array of already-chosen numbers.
Here's a code example which illustrates this idea, picking numbers between 0 and 9, not allowing repeat numbers. The process is broken down below.
An array,
alreadyPicked, is declared. It serves to keep track of which numbers have already been selected, so that they won't be selected twice.The number
maxis declared. It is used to prevent an infinite loop when there are no more random numbers to choose.Random numbers are chosen in the
whileloop, which loops either until theuniqueboolean is set totrueor until the length of thealreadyPickedarray has reached themaxlength, which happens when there are no more unique numbers to select.Once a number is obtained, the statement
alreadyPicked.includes(randNumber)checks to see whether therandNumberis among those numbers already selected and stored inalreadyPicked.If this is false, this means a unique number has been selected.
uniqueis then set to true to break the loop, and the number is pushed toalreadyPickedso that it won't be selected again.