Workshop › Minesweeper › Step 7 of 26
Right now the mines always fill the first rows. Shuffling the candidates first (the Fisher–Yates shuffle) makes every layout equally likely.
```js
for (let i = candidates.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[candidates[i], candidates[j]] = [candidates[j], candidates[i]]
}
```i--).Math.random() is a number from 0 up to 1; times i + 1, rounded down, it is a whole number from 0 to i.[a, b] = [b, a] swaps two items. The ; in front stops the line from joining the one above.In placeMines, under the candidates line, write the loop. Press Run.
Skills: Loops, Arrays and objects
← Mines, but not under the first click Count the mines around →