Random Shuffling isn't Easy! - Friday Minis 316

Поделиться
HTML-код
  • Опубликовано: 13 июл 2024
  • When using languages like Python, we often don't use convenience functions without knowing what complex stuff goes on behind the hood. Today, we look at python's random.shuffle() function, and what hoops we'd have to jump through to implement it ourselves!
    = CREDITS =
    My appreciation extends to the creators of the following external assets that made this video possible:
    Blown Away by Kevin MacLeod (incompetech.com)
    Licensed under Creative Commons: By Attribution 3.0
    creativecommons.org/licenses/b...
    ISRC: USUAN1200100
    = 0612 TV =
    0612 TV, a sub-project of NERDfirst.net, is an educational RUclips channel. Started in 2008, we have now covered a wide range of topics, from areas such as Programming, Algorithms and Computing Theories, Computer Graphics, Photography, and Specialized Guides for using software such as FFMPEG, Deshaker, GIMP and more!
    Enjoy your stay, and don't hesitate to drop me a comment or a personal message to my inbox =) If you like my work, don't forget to subscribe!
    Like what you see? Buy me a coffee → www.nerdfirst.net/donate/
    0612 TV Official Writeup: nerdfirst.net/0612tv
    More about me: about.me/lcc0612
    Official Twitter: / 0612tv
    = NERDfirst =
    NERDfirst is a project allowing me to go above and beyond RUclips videos into areas like app and game development. It will also contain the official 0612 TV blog and other resources.
    Watch this space, and keep your eyes peeled on this channel for more updates! nerdfirst.net/
    -----
    Disclaimer: Please note that any information is provided on this channel in good faith, but I cannot guarantee 100% accuracy / correctness on all content. Contributors to this channel are not to be held responsible for any possible outcomes from your use of the information.

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

  • @Maazin5
    @Maazin5 3 года назад +2

    Found your channel today and this was a great video to follow along to.

    • @NERDfirst
      @NERDfirst  3 года назад +2

      Hello and thank you very much for your comment! Glad you like my work =)

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

    Great video and thank you for explaining how this works.

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

      You're welcome! Glad you liked the video =)

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

    I thought you will explain how rand() method works :)

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

      Hello and thank you for your comment! Anything about rand() you'd like me to clarify?

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

    Hello, i started to learn programming recently and i dont understand why you choose other language to write this function. Is python dont let you to write it using "for" function?
    I am trying to make .shuffle with "for" but i cant understand how to manage with repeating elements.

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

      Hello and thank you for your comment! The same can absolutely be done with Python - I chose C because it's a lower-level language that doesn't give us things like random.shuffle(). Of course, if you're already using Python then you can just call random.shuffle() directly, but the algorithm given here can be implemented in any language.

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

    Why not use the entire array index range for the new index? Only using indexes behind seems bad for the end of the array and is more computation.

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

      Hello and thank you for your comment! You could certainly do a fixed number of random swaps across the entire array, and you'd get a shuffled result too. My goal here was to make it fair for every element in that every index is considered once for the swap.

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

      @@NERDfirst but if your are realy unlucky you will always get the last index. And the more you process the higher the chance to get the last index as the possible numbers get reduced

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

      That's absolutely true! I'm not trying to guarantee that the list does gets shuffled. I mean, after all, getting the exact same list back is a possible result, and if my probability and statistics isn't too rusty, it's equally likely as getting any other combination.
      Having said that, I can't confirm that my technique here gives rise to all possible combinations in equal probability!

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

    4:57 rand(3) returns integers up to RAND_MAX which, as you noticed, can be quite small man7.org/linux/man-pages/man3/rand.3.html . That man page recommends using something like random(3) instead.

    • @NERDfirst
      @NERDfirst  3 года назад +3

      Hello and thank you for your comment and for sharing! This is a toy example so in fact RAND_MAX is more than enough for such an application. But perhaps someone else doing more complex random number generation in C might find this useful!

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

      If you want something more like cryptographically strong random or pseudorandom numbers, then perhaps use /dev/urandom or /dev/random linux.die.net/man/4/urandom .