Fisher-Yates Shuffle Modern Algorithm JavaScript Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 22 янв 2025

Комментарии • 144

  • @jayesh1891
    @jayesh1891 Год назад +26

    even chatgpt couldn't beat this 9yo explanation 👏

    • @AdamKhoury
      @AdamKhoury  Год назад +5

      that's cool to hear!

    • @eledorusso
      @eledorusso 10 месяцев назад

      it is very true. I came here after chat gpt
      @@AdamKhoury

    • @WyMustIGo
      @WyMustIGo 9 месяцев назад

      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.

  • @gabsfrmarqs
    @gabsfrmarqs 3 года назад +10

    I'm not studying JavaScript right now but i really liked your explanation about how the Fisher-Yates algorithm works, thanks :)

  • @MikeEarleyfivestar
    @MikeEarleyfivestar 3 месяца назад

    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.

  • @nugaskliping
    @nugaskliping Год назад +4

    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

  • @hassan-rakib
    @hassan-rakib 3 года назад +1

    Oh my gosh! Finally I know how the Fisher-Yates method works. 🙂

  • @TraceguyRune
    @TraceguyRune 6 лет назад +27

    The paper visualization was awesome. I was reading a documentation on this and was confused.

  • @milanbudha3381
    @milanbudha3381 4 года назад +1

    Nobody was able to make understand this topic but you did it so simply..Thank you so much

  • @sabitbayar9256
    @sabitbayar9256 2 года назад +1

    I understood its logic in first 1.30 mins. thank u so much

  • @76ers
    @76ers 11 лет назад +3

    The visual explanation is awesome! Please incorporate it whenever you can in future videos.

  • @ShidehNaderi
    @ShidehNaderi 11 лет назад +7

    Your explanation is very very very clear. Thank you.

  • @AdamKhoury
    @AdamKhoury  11 лет назад +14

    Fisher-Yates Shuffle Modern Algorithm JavaScript Programming Tutorial

  • @Rhua_Raj
    @Rhua_Raj 8 месяцев назад

    great job it's been 10 years but still life saving tutotial

  • @stacyjean-philippe2477
    @stacyjean-philippe2477 7 лет назад +14

    Thank you so much for explaining this so well! I've been trying to wrap my head around this for the longest time.

    • @AdamKhoury
      @AdamKhoury  7 лет назад

      No problem Stacy. Thanks for the kind feedback.

  • @samuelobimakinde3305
    @samuelobimakinde3305 4 года назад +1

    The video was instructive. Thanks, Khoury

  • @heavydirtysoul1491
    @heavydirtysoul1491 3 года назад +1

    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

    • @heavydirtysoul1491
      @heavydirtysoul1491 3 года назад

      I have some second thoughts about this algorithm. Isn't the start of the array shuffled more actively than the end?

    • @Neko_Necromancer
      @Neko_Necromancer 21 день назад

      ​@@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

  • @sayedpritom
    @sayedpritom 4 года назад

    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

  • @H0b0Gamers
    @H0b0Gamers 11 лет назад +1

    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!

  • @seemsas
    @seemsas 11 лет назад +2

    I really like your tutorials, I've learned so much by them. Thanks!

  • @kennethf6266
    @kennethf6266 8 лет назад

    This guy's a great teacher! Very useful stuff.

  • @YannisStarwalker
    @YannisStarwalker 11 лет назад +3

    Thanks for another awesome tutorial, Adam!!

  • @GeorgeHarrisonJr
    @GeorgeHarrisonJr 6 лет назад +1

    Thank you for this! By far one of the best explanations. Excellent !

  • @RagePeanut
    @RagePeanut 8 лет назад

    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 !

  • @dougreiserrelief694
    @dougreiserrelief694 6 лет назад +2

    Thank you for the code and good examples and an explanation that I understood.

  • @AllyNyan
    @AllyNyan 7 лет назад +2

    Very instructive video I didn't understand the wikipedia page but now I do

  • @Revelatus
    @Revelatus 5 лет назад +2

    Great explanation of the algorithm, thank you

  • @ezekielthemack
    @ezekielthemack 11 лет назад +4

    Excellent tutorial as always. Thanks.

  • @victorsuarez3
    @victorsuarez3 Год назад +1

    Thank you so much for this. Amazing and simple explanation.

  • @Sumdottycoy
    @Sumdottycoy 9 лет назад +5

    Awesome explanation. Implemented into my C game. :)

  • @kristenkumagai1791
    @kristenkumagai1791 8 лет назад

    Really appreciate you posting this tutorial. Thank you!

  • @richardramos7267
    @richardramos7267 6 лет назад +3

    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..

  • @dianaloredanasebo7215
    @dianaloredanasebo7215 7 лет назад +2

    Great explanation. Thank you so much, sir! :)

  • @Thikondrius
    @Thikondrius Год назад

    At 00:28 the letters swaped (probably a wrong cut), be carefull ^^. Thanks for the video it's super clear.

  • @chrismaki5456
    @chrismaki5456 2 года назад +1

    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.

  • @glendonphilippbaculio1811
    @glendonphilippbaculio1811 8 лет назад

    your tutorials are great! thanks

  • @reromsp
    @reromsp Год назад +1

    Thats a great explaination!! Thankyou

  • @asahifactory
    @asahifactory 6 лет назад

    Thank you so so much! Finally I understand what is going on there!

  • @expukpuk
    @expukpuk 6 лет назад +1

    Thanks, mate. Nice piece of code.

  • @dr.sleaseball441
    @dr.sleaseball441 3 года назад

    i try to learn JS. this was helpful, very well explained!

  • @riiad
    @riiad 4 года назад +1

    I wish I could give this vid more than one thumbs-up

  • @vadimmalagurski5731
    @vadimmalagurski5731 7 лет назад

    Thanks for sharing your knowledge very clear explanation !

  • @usenli
    @usenli 2 года назад

    Thanks friend, very usufull visual explication

  • @kevnar
    @kevnar 4 года назад

    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.

    • @samuelobimakinde3305
      @samuelobimakinde3305 4 года назад

      I'm just learning programming, Kevnar. Your point is plausible. Can you please send me a code on how to do what you suggested?

  • @christophervistal6036
    @christophervistal6036 6 лет назад +1

    Thank you for this wonderful explanation :D

  • @m.sr.3088
    @m.sr.3088 2 года назад

    well done best example ever ....bwosss!!!

  • @WinLoveCry
    @WinLoveCry 9 лет назад

    Great stuff! Very informative.

  • @dankomilutinovic6792
    @dankomilutinovic6792 8 лет назад

    Thanks for this awesome tutorial!

  • @hwangi0404
    @hwangi0404 9 лет назад +1

    As a beginner, my mind is fucking blown. Wow, holy crap!!!

  • @JM-jc1vz
    @JM-jc1vz 7 лет назад

    Good clear explanation, thank you.

  • @PaulSebastianM
    @PaulSebastianM 6 лет назад

    Dis array is everywhere!

  • @jaredroberge9957
    @jaredroberge9957 5 лет назад

    Solid video still. Been playing around with this particular shuffling method in c# for a unity project

  • @souravsuman2633
    @souravsuman2633 4 года назад

    Thank you so much .....really good explanation

  •  11 лет назад

    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.

  • @HarshDoes
    @HarshDoes Год назад

    Thank you so much for this 🙌

  • @antoniocardenasfuentes1627
    @antoniocardenasfuentes1627 7 лет назад

    Thank you so much! Great explanation.

  • @hengamehoseini2666
    @hengamehoseini2666 9 лет назад

    Great explanation! thank you!

  • @TkJarok
    @TkJarok 6 лет назад

    Excelente! perfecto para aplicarlo en mi tarea de Polibios!

  • @sumangeorge7
    @sumangeorge7 4 года назад

    perfect.. thanks for the upload.

  • @dakota7369
    @dakota7369 5 месяцев назад

    Does everything have the same odds of being chosen?

  • @davidjames1684
    @davidjames1684 6 лет назад

    If the random number is a 2 or a 1, we go to index position 1? I dont think so!

  • @talentacquisition1746
    @talentacquisition1746 9 лет назад

    How can you add breaks to the results? Or can I call the results some how and style them?

  • @limasntoinf
    @limasntoinf 6 лет назад +1

    Awesome. Thank you.

  • @maracamo
    @maracamo 10 лет назад

    Great, really good!!!!

  • @Ginfio
    @Ginfio 2 года назад

    great explanation

  • @daisoreanulaurentiu3943
    @daisoreanulaurentiu3943 10 лет назад

    such a shame that you have such a few views on such great tutorials

  • @fatmanur5409
    @fatmanur5409 Год назад

    What if all the random index values are equal to the original index and all the variables stay in the same indices?

    • @Oliveira529
      @Oliveira529 Год назад

      Depending on the number of elements, it is extremely unlikely

    • @AdamKhoury
      @AdamKhoury  Год назад

      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.

  • @VDeshm
    @VDeshm 11 лет назад

    Awesome tutorial....

  • @eledorusso
    @eledorusso 10 месяцев назад +1

    incredible thank u

  • @fragrantbloom
    @fragrantbloom 2 года назад

    Shouldn"t i be arr.length-1 ? Thank you for the great explanation!

    • @chiggywiggy524
      @chiggywiggy524 Год назад

      Maybe it has to do with the fact that he's use prefix --i instead of postfix i-- to decrement the value

  • @teebee5739
    @teebee5739 9 лет назад

    Awesome video

  • @LadyVibora69
    @LadyVibora69 6 лет назад +1

    Thank you I understand all ^^ Thanks a lot really :D

  • @jhogerguerrero2032
    @jhogerguerrero2032 8 лет назад

    Excellent!!!

  • @frankwang1165
    @frankwang1165 11 лет назад +2

    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

  • @WanderSoulLife
    @WanderSoulLife 6 лет назад +1

    Hi thanks for this beautifully explained video. Can we use this algorithm for shuffling data sets for data analysis?

    • @ARandomPerson--
      @ARandomPerson-- 3 года назад

      If you put all the data sets into an array I don't see why not

  • @natalieding5265
    @natalieding5265 6 лет назад

    why do we need swap process?

  • @boricua12903
    @boricua12903 6 лет назад

    OMG thank you for this video.

  • @43i3adam4
    @43i3adam4 6 лет назад +1

    Thanks a lot; very clear

  • @WanderSoulLife
    @WanderSoulLife 6 лет назад

    Thanks for this video. Can we use this method for shuffling a dataset?

  • @vladankd
    @vladankd 6 лет назад +1

    This is great thanks

  • @adambach5721
    @adambach5721 4 года назад

    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?

    • @adambach5721
      @adambach5721 4 года назад

      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

  • @livram55
    @livram55 4 года назад

    good explanation

  • @abbi1750
    @abbi1750 8 лет назад

    Great explanation... tnx

  • @JNET_Reloaded
    @JNET_Reloaded 4 года назад

    Can you make a tut on how to make anagrams from words using a dictonary file and shuffle etc?

  • @AlastairGames
    @AlastairGames 9 дней назад

    thanks well explained

  • @Alacrity420
    @Alacrity420 9 лет назад +3

    awesome

  • @bmnbl
    @bmnbl 11 лет назад

    Nice one.

  • @joelsukumar6319
    @joelsukumar6319 7 лет назад

    in the j variable , why write (i+1) ?

    • @pilyglot3037
      @pilyglot3037 6 лет назад +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 :-).

  • @toby1kenobe
    @toby1kenobe 11 лет назад

    where is this used? I assume its common?

  • @thepvporg
    @thepvporg 7 лет назад

    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...

  • @grmmhp
    @grmmhp 7 лет назад

    I'm not flawless, but you know, I got a diamond heart.

  • @davidjames1684
    @davidjames1684 6 лет назад

    It took 2 guys to come up with this simple algorithm? Was it too much work for just 1 person?

  • @guillemvila5960
    @guillemvila5960 4 года назад

    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.

  • @jakubklos3438
    @jakubklos3438 6 лет назад

    Hi guys!!! Could anyone please explane me the last line of the code arr[i] = temp; :(

    • @AdamKhoury
      @AdamKhoury  6 лет назад

      arr[i] = temp... means that the value at index "i" is going to be replaced with the temporary value.

  • @gosinP
    @gosinP 4 года назад

    Thank you.

  • @titaniumwolf2
    @titaniumwolf2 9 лет назад

    thank you very much.

  • @cayao2001
    @cayao2001 11 лет назад

    When or what would I use this algorithm for?

    • @claudiovillani8339
      @claudiovillani8339 7 лет назад +1

      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.

  • @moideenferoke5955
    @moideenferoke5955 11 лет назад

    very nice

  • @priyamvashi2187
    @priyamvashi2187 3 года назад

    Thank you

  • @machan2524
    @machan2524 7 лет назад

    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.

  • @iLLestTv
    @iLLestTv 6 лет назад +1

    Help my black ass out alot. Needed this code for a game Im programming my g.

  • @TheRojo387
    @TheRojo387 2 года назад

    This is good for implementing card games.