ChatGPT is garbage, it isn't AI. I refer to ChatGPT as a smart filter because all it does is "filter" data that was scraped from the internet, it cannot make sense of anything that it did not get from the internet. There is a HUGE issue with this because most of the stuff on the internet is either factually incorrect, incomplete, biased, or injected to push an agenda. And, well, garbage in means garbage out. I have shown some blatant examples of how bad it is in various videos.
OK I know it's been 11 years since you posted this video, but I deeply appreciate how well you explained this, starting with the physical manipulatives and moving to the code. This gave me a very good understanding of the functionality, thank you.
@@heavydirtysoul1491 Yes, and that's the entire point. Every position is given exactly one opportunity to be swapped into any particular other position. If you let them swap anywhere, that actually makes it less random
Really Really Great Video. Your 2013 Video Tutorial Helped Me In 2020! & It Totally Felt Like A Tutorial of 2020. Many Thanks For Such Detailed & Consistent Explanation. The Concept Is Very Clear To Me Now. Alhamdulillah
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..
I understand the explanation of what is happening when the code is executed (physically how it reshuffles within itself) but I'm still lost on the variables and their values and how they make it happen.
Why decrement the range of the random number as you step down the loop? Why not just swap anywhere within the array? Wouldn't that give more randomness? It's not like it's saving time or memory to reduce the range.
One can record the original array into a temporary variable. Once the shuffle ends, one can compare the two arrays to see if they exactly match. If there is an exact match, run it through shuffle again. Oliveira529 is correct, extremely unlikely, but the above approach can double check it.
very instructive even for a beginner, what I'm interested is how to make a subtitle for the video in different languages? it would very helpful for those non-english speakers to understand the conversation in a video with captions of both original and translated Frank Wang
in the first loop of the "while" i=7 the maximum value may be 8 because we add 1 to the i and the random can also be 1, the j must be 0-7 and it looks like it can be 8 when we use (i+1), am I right or wrong?
Math.random() generates a number that is more than 0 and less than 1 (for example, I just ran it and got 0.811115096298769). If you multiply that number by, say, 10, you'll get a number *less* than ten (in this example, 8.111...). So if you want the result to possibly _be_ 10, you have to multiply the random number by 11 (and of course Math.floor() cuts off the extra decimals). In this case, the programmer wants the selected index to be included in the randomization, so the random number must be multiplied by 1 more than the index, i.e. (i+1). I hope that made sense :-).
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...
In case that the "Math.random" number is 1 in the first pass of the loop using the formula j = Math.floor (Math.random() * (i+1)); the result number would be 8. Wouldn't, then, return undefined? what about j = Math.floor (Math.random() * (max - min)) + min; ? The max value will always be 7. I'm learning to code and maybe I'm just talking nonsense.
making a card game that has to generate a hand for each player. Say you need a hand of 8 cards out of a deck of 52, you would use use this to shuffle the deck first, and then pull 8 cards from the shuffled array.
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.
even chatgpt couldn't beat this 9yo explanation 👏
that's cool to hear!
it is very true. I came here after chat gpt
@@AdamKhoury
ChatGPT is garbage, it isn't AI. I refer to ChatGPT as a smart filter because all it does is "filter" data that was scraped from the internet, it cannot make sense of anything that it did not get from the internet. There is a HUGE issue with this because most of the stuff on the internet is either factually incorrect, incomplete, biased, or injected to push an agenda. And, well, garbage in means garbage out. I have shown some blatant examples of how bad it is in various videos.
I'm not studying JavaScript right now but i really liked your explanation about how the Fisher-Yates algorithm works, thanks :)
OK I know it's been 11 years since you posted this video, but I deeply appreciate how well you explained this, starting with the physical manipulatives and moving to the code. This gave me a very good understanding of the functionality, thank you.
Something like this is what make me praise people of the past that invents like everything that can't be loaded by my slow brain
Oh my gosh! Finally I know how the Fisher-Yates method works. 🙂
The paper visualization was awesome. I was reading a documentation on this and was confused.
Nobody was able to make understand this topic but you did it so simply..Thank you so much
I understood its logic in first 1.30 mins. thank u so much
The visual explanation is awesome! Please incorporate it whenever you can in future videos.
Your explanation is very very very clear. Thank you.
Fisher-Yates Shuffle Modern Algorithm JavaScript Programming Tutorial
great job it's been 10 years but still life saving tutotial
Thank you so much for explaining this so well! I've been trying to wrap my head around this for the longest time.
No problem Stacy. Thanks for the kind feedback.
The video was instructive. Thanks, Khoury
No sweat Samuel.
Author has done some great work. Thank you for creating this excellent tutorial. Even in 2021 it is actual same as it was in 2013
I have some second thoughts about this algorithm. Isn't the start of the array shuffled more actively than the end?
@@heavydirtysoul1491 Yes, and that's the entire point. Every position is given exactly one opportunity to be swapped into any particular other position. If you let them swap anywhere, that actually makes it less random
Really Really Great Video. Your 2013 Video Tutorial Helped Me In 2020! & It Totally Felt Like A Tutorial of 2020. Many Thanks For Such Detailed & Consistent Explanation. The Concept Is Very Clear To Me Now. Alhamdulillah
Awsome, more stuff like this please! Just the explanation alone was awsome. I saw the first part then wrote it in php before watching your code!
I really like your tutorials, I've learned so much by them. Thanks!
This guy's a great teacher! Very useful stuff.
Thanks for another awesome tutorial, Adam!!
Thank you for this! By far one of the best explanations. Excellent !
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 !
Thank you for the code and good examples and an explanation that I understood.
Very instructive video I didn't understand the wikipedia page but now I do
Great explanation of the algorithm, thank you
Excellent tutorial as always. Thanks.
Thank you so much for this. Amazing and simple explanation.
Glad you enjoyed it!
Awesome explanation. Implemented into my C game. :)
What game did you make?
Really appreciate you posting this tutorial. 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..
Great explanation. Thank you so much, sir! :)
At 00:28 the letters swaped (probably a wrong cut), be carefull ^^. Thanks for the video it's super clear.
I understand the explanation of what is happening when the code is executed (physically how it reshuffles within itself) but I'm still lost on the variables and their values and how they make it happen.
your tutorials are great! thanks
Thats a great explaination!! Thankyou
Glad it was helpful!
Thank you so so much! Finally I understand what is going on there!
Thanks, mate. Nice piece of code.
i try to learn JS. this was helpful, very well explained!
I wish I could give this vid more than one thumbs-up
Thanks for sharing your knowledge very clear explanation !
Thanks friend, very usufull visual explication
Why decrement the range of the random number as you step down the loop? Why not just swap anywhere within the array? Wouldn't that give more randomness? It's not like it's saving time or memory to reduce the range.
I'm just learning programming, Kevnar. Your point is plausible. Can you please send me a code on how to do what you suggested?
Thank you for this wonderful explanation :D
No problem buddy.
well done best example ever ....bwosss!!!
Great stuff! Very informative.
Thanks for this awesome tutorial!
As a beginner, my mind is fucking blown. Wow, holy crap!!!
Good clear explanation, thank you.
Dis array is everywhere!
Solid video still. Been playing around with this particular shuffling method in c# for a unity project
Thank you so much .....really good explanation
as random() is used how can i mke this a encrypt / decrypt algo? or can you make a tut for this i want a custom style algo for enc and dec.
Thank you so much for this 🙌
Thank you so much! Great explanation.
Great explanation! thank you!
Excelente! perfecto para aplicarlo en mi tarea de Polibios!
perfect.. thanks for the upload.
Does everything have the same odds of being chosen?
If the random number is a 2 or a 1, we go to index position 1? I dont think so!
How can you add breaks to the results? Or can I call the results some how and style them?
Awesome. Thank you.
Great, really good!!!!
great explanation
such a shame that you have such a few views on such great tutorials
What if all the random index values are equal to the original index and all the variables stay in the same indices?
Depending on the number of elements, it is extremely unlikely
One can record the original array into a temporary variable. Once the shuffle ends, one can compare the two arrays to see if they exactly match. If there is an exact match, run it through shuffle again. Oliveira529 is correct, extremely unlikely, but the above approach can double check it.
Awesome tutorial....
incredible thank u
Shouldn"t i be arr.length-1 ? Thank you for the great explanation!
Maybe it has to do with the fact that he's use prefix --i instead of postfix i-- to decrement the value
Awesome video
Thank you I understand all ^^ Thanks a lot really :D
Excellent!!!
very instructive even for a beginner, what I'm interested is how to make a subtitle for the video in different languages? it would very helpful for those non-english speakers to understand the conversation in a video with captions of both original and translated Frank Wang
Hi thanks for this beautifully explained video. Can we use this algorithm for shuffling data sets for data analysis?
If you put all the data sets into an array I don't see why not
why do we need swap process?
OMG thank you for this video.
Thanks a lot; very clear
no sweat partner
Thanks for this video. Can we use this method for shuffling a dataset?
This is great thanks
in the first loop of the "while" i=7 the maximum value may be 8 because we add 1 to the i and the random can also be 1, the j must be 0-7 and it looks like it can be 8 when we use (i+1), am I right or wrong?
I found out, the Math.random () is from 0 (inclusive) to 1(EXCLUSIVE), to be able to use j=0-7 we must have floor of 0-7.9999
good explanation
Great explanation... tnx
Can you make a tut on how to make anagrams from words using a dictonary file and shuffle etc?
thanks well explained
awesome
Nice one.
in the j variable , why write (i+1) ?
Math.random() generates a number that is more than 0 and less than 1 (for example, I just ran it and got 0.811115096298769). If you multiply that number by, say, 10, you'll get a number *less* than ten (in this example, 8.111...). So if you want the result to possibly _be_ 10, you have to multiply the random number by 11 (and of course Math.floor() cuts off the extra decimals). In this case, the programmer wants the selected index to be included in the randomization, so the random number must be multiplied by 1 more than the index, i.e. (i+1). I hope that made sense :-).
where is this used? I assume its common?
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...
I'm not flawless, but you know, I got a diamond heart.
It took 2 guys to come up with this simple algorithm? Was it too much work for just 1 person?
In case that the "Math.random" number is 1 in the first pass of the loop using the formula j = Math.floor (Math.random() * (i+1)); the result number would be 8. Wouldn't, then, return undefined? what about j = Math.floor (Math.random() * (max - min)) + min; ? The max value will always be 7. I'm learning to code and maybe I'm just talking nonsense.
Hi guys!!! Could anyone please explane me the last line of the code arr[i] = temp; :(
arr[i] = temp... means that the value at index "i" is going to be replaced with the temporary value.
Thank you.
thank you very much.
When or what would I use this algorithm for?
making a card game that has to generate a hand for each player. Say you need a hand of 8 cards out of a deck of 52, you would use use this to shuffle the deck first, and then pull 8 cards from the shuffled array.
very nice
Thank you
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.
Help my black ass out alot. Needed this code for a game Im programming my g.
This is good for implementing card games.