Enhance your coding skillset by learning how to implement the Fisher-Yates Shuffle, a classic coding algorithm used for randomizing array items. This tutorial, presented by JavaScript and Python instructor, Brian McClain, takes a practical approach to explain this algorithm in the context of a memory game built in JS.
Key Insights
- Brian McClain, an instructor at Noble Desktop in New York City, teaches JavaScript and Python programming.
- The Fisher-Yates Shuffle is a coding algorithm used to randomize items in an array.
- The shuffle algorithm is applied in a memory game where the goal is to remember and match icons that appear and disappear.
- In the game's code, the Fisher-Yates Shuffle is used to randomize the order of the 30 items in an array.
- The shuffle works by saving the current item to a temporary variable, replacing the current item with a random item from the array, and then assigning the temporary item back to the random position.
- Noble Desktop offers bootcamps in JavaScript Full-stack Web Development and Python Data Science, as well as other subjects, both in-person and live online.
How Fisher-Yates Works
-
Iterate Backward
for (let i = arr.length - 1; i > 0; i--).
-
Pick a Random Index
const j = Math.floor(Math.random() * (i + 1)).
-
Swap in Place
[arr[i], arr[j]] = [arr[j], arr[i]] — destructuring trick.
-
Result: Uniform Shuffle
Every permutation equally likely — superior to .sort(() => Math.random() - 0.5).
Video Transcription
Hello. My name is Brian McClain. I'm an instructor in JavaScript and Python programming at Noble Desktop in New York City. Today we're going to look at a classic coding algorithm called the Fisher-Yates Shuffle, which is used for randomizing the items of an array. You can read all about it on Wikipedia, but instead, we're going to just dive right in and look at a practical implementation of it.
So let's say you have a memory game such as this, which I made in JS, and you have to memorize the position of all these icons and make pairs out of them. I think I saw the apples and the baseballs and I don't remember any of the other stuff, but you could track it down and try to remember where things are as they come up and disappear.
But if we load it all again, things get moved. Now the apples are over here and the baseballs are down here. Where are you then? Okay, great. And I don't remember anything else. And if you load it again, everything is somewhere else, Let's check out the code. So, we have an array of the 30 items, alphabetized. We use the name in the array to load the image.
But, of course, it wouldn't be much of a memory game if all the items were in alphabetical order coming into the grid, so the first order of business is we have to shuffle it, and randomize all these items. To do that we use the Fisher-Yates Shuffle. We need a loop now. We're going to say it equals zero.
It is less than game icons that will I a plus classic for a loop every time through we're going to save the current item to a temporary variable. Then we're going to override the current item with a random item and then assign the temp item back to the random position—basically swapping them. We will generate a random integer in the range of the array index, which is 0 to 29.
Let's say: let r equal math-dot-floor (Math.floor()) math-dot-random(Math.random()) times gameIcons dot length (gameIcons.length). This gives us an integer in the 0 to 29 range. And then we're going to set the current item, which is gameIcons[i], equal to a temp variable. Anchor is the first item, and let's say the first random number is 24—the index of the seahorse. In that case, we would replace the anchor with the seashorse.
But then we would have two seahorses, so that's why we saves the anchor to the variable called temp. So then, we replace the seahorse at index 24 with temp, which is set equal to anchor. And with that, the anchor at index 0 has been swapped with the seahorse at index 24.
To see if it works, we'll console.log the array, gameIcons, and check the console. There it is: totally randomized: So it worked! Thank you very much for watching;; that concludes this presentation on the Fisher-Yates algorithm. My name is Brian McClain. I am with Noble Desktop in New York, where we have JavaScript Full-stack Web development bootcamp and a Python Data Science bootcamp, among other subjects that we teach, both in-person and live online. Thanks for watching.
Video Transcription:
hello my name is Brian McLean I'm an instructor of JavaScript and Python Programming at Noble desktop in New York City today we're going to look at a classic coding algorithm called the Fisher Yates Shuffle which is used for randomizing the items of an array you can read all about it at Wikipedia but instead we're going to just Dive Right In look at a practical implementation of it so let's say you have a memory game such as this which I made in JS and you have to memorize the position of all these icons and make pairs out of them so I think I saw the apples and The Baseballs and I don't remember any of the other stuff but you could track it down and try to remember where
things are as they come up and disappear right but if we were load it all again the things have moved now the apples are over here and The Baseballs are down here where are they okay great and I don't remember anything else right and if you're load it again everything's somewhere else let's check out the code so we have an array of the 30 items alphabetized use the name in the array to load the image but of course it wouldn't be much of a memory game to fall the items were in alphabetical order right coming into the grid so the first order of businesses we have to shuffle and randomize all these items so to do that we use the fishery Eight's Shuffle right we need to we need
a loop now so we're going to say for let I equals zero I is less than game icons dot length I plus plus classic for Loop every time through we're going to save the current item to a temporary variable and then we're going to overwrite the current item with a random item and then assign the temp item back to the random position basically swapping them but so it'll generate a random INT in the range of the array index which is 0 to 29 so we'll say let r equal map that floor mat dot random times game icons dot length this gives us an integer in the 0 to 29 range and then we're going to say the current item which is game icons I equals the randomly chosen item which is game icons
are so if you generated at 24 the C the seahorse would be put over in place of the anchor and then the anchor would come into the place of the seahorse but now we have two seahorses right so that's why we saved the original item the anchor it's a temp so we'll say game icons are say R is 24 that's our seahorse that's going to equal temp which is our anchor first time through let's just associate our comments with code correctly and replace the random item with the current item as temp to see if it worked we'll go console log the array game icons come over to our page check the console there is totally randomized Barn slam dunk basketball so it worked well thank you very much
for watching that concludes this presentation on the Fisher Yates algorithm my name is Brian McLean I am with Noble desktop in New York where we teach JavaScript development and python data science among other subjects thanks for watching
