Lesson Code: http://www.developphp.com/video/JavaScript/Fisher-Yates-Shuffle-Modern-Algorithm-Array-Programming-Tutorial
In this programming exercise we will demonstrate the concepts behind the Fisher-Yates Modern Shuffle algorithm because we are going to use its logic to program a shuffle method into JavaScript. Using a physical example on the table we will convey the logic behind the algorithm and discuss the concept, then we will write the logic in JavaScript programming to add an array shuffle method to JavaScript’s array object.
Original source
49 responses to “Fisher-Yates Shuffle Modern Algorithm JavaScript Programming Tutorial”
Thanks for this video. Can we use this method for shuffling a dataset?
Dis array is everywhere!
Thank you so so much! Finally I understand what is going on there!
why do we need swap process?
Hi thanks for this beautifully explained video. Can we use this algorithm for shuffling data sets for data analysis?
The paper visualization was awesome. I was reading a documentation on this and was confused.
It took 2 guys to come up with this simple algorithm? Was it too much work for just 1 person?
Each time it will be different? Wrong for the 3rd time. It is possible the same random numbers will be generated. That is strike 3!
If the random number is a 2 or a 1, we go to index position 1? I dont think so!
Not all arrays start at index 0. Downvoted for inaccuracy.
Excelente! perfecto para aplicarlo en mi tarea de Polibios!
OMG thank you for this video.
Thank you for this wonderful explanation 😀
Thank you for the code and good examples and an explanation that I understood.
Awesome. Thank you.
Thank you very much, the first time I've watch this I only watch the visual representation, and then I program it my own, I made it by looping the array from the end to 1, then inside that I made another loop, so it is nested loop, every time I loop it the end array will be replace inside the nested array with the random place of the array… But this method is more efficient and less code, It is really hard for me to understand, It takes me almost 2 days, dreaming about this problem, T_T but now I get it..
Help my black ass out alot. Needed this code for a game Im programming my g.
Hi guys!!! Could anyone please explane me the last line of the code arr[i] = temp; 🙁
Thanks a lot; very clear
Thank you I understand all ^^ Thanks a lot really 😀
Thank you for this! By far one of the best explanations. Excellent !
This is great thanks
Thanks, mate. Nice piece of code.
in the j variable , why write (i+1) ?
Thank you so much for explaining this so well! I've been trying to wrap my head around this for the longest time.
Good clear explanation, thank you.
Thank you so much! Great explanation.
I wonder why A and G were swapped for the 3rd random number 5. Index position should be moved to E without moving G. That is, the last 3 alphabets of shuffled array are GBD.
Thanks for sharing your knowledge very clear explanation !
Great explanation. Thank you so much, sir! 🙂
Excellent!
Very instructive video I didn't understand the wikipedia page but now I do
The prototype method is a good idea but for some people, they may get confused by it as I know from experience of tying to demonstrate to people in a web developer forum I frequent.
I have used a different method of shuffling and one that uses a random number that is 0 to 'nth' where the random selects the array element, in my routine I remove the element and put it in to a results array, the routine runs for as many elements in the array, the result is very compact code and like most forums, people go on about performance, which would have been relevant 20 years ago but pose no issues with todays computers.
—————————————————————————-
Array.prototype.shuffle = function(){
var s = [];
while( this.length ) s.push( this.splice( -(~~(Math.random()*this.length)|0),1) );
return s;
}
testarray = ["some","data","is","here","so","we","can","shuffle","items","randomly"].shuffle();
—————————————————————————-
IMHO it is much faster to pull a set of elements out of an array by a random method than swapping, in the method I posted, your using a new array to store the results sequentially and the original array is reducing and the range of randoms gets lower as it would in the moving a pointer, the efficiency arguments and claims by some people are IMHO unfounded and when you examine the different methods, they are essentially doing the same thing. I have even weighted the random function to output a negative integer to pull from the end of the array like in the Fisher Yates demo you just gave.
The output will produce something like [randomly, items, so, shuffle, we, some, can, here, is, data] and [we, so, randomly, can, here, data, some, shuffle, is, items] as an output. If your really paranoid that the sort is not random enough, you can run the shuffle twice or more times if you like…
thank you very much in 2017
I'm not flawless, but you know, I got a diamond heart.
TNAKS YOUUUUUUUUUUUUUUUUU
Woody Allen teaches computer programming.
thank you :)!!!!!
your tutorials are great! thanks
Really appreciate you posting this tutorial. Thank you!
Excellent!!!
This guy's a great teacher! Very useful stuff.
Thanks for this tutorial, been trying to do that today without any success. Thanks to you I've managed to do it, really well explained !
/* shuffle array */
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i -= 1) {
j = Math.floor(Math.random() * i);
x = a[i – 1];
a[i – 1] = a[j];
a[j] = x;
}
}
Great explanation… tnx
Thanks for this awesome tutorial!
Awesome video
How can you add breaks to the results? Or can I call the results some how and style them?
How can you add breaks to the results? Or can I call the results some how and style them?