JDSherbert / Fisher-Yates-Shuffle

Fisher Yates Algorithm implementation for randomizing an array of objects, implemented in various languages.

Home Page:https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

image

Fisher Yates Shuffle Algorithm

Stars Badge Forks Badge Watchers Badge Issues Badge


C++ Template License




Overview

The Fisher-Yates shuffle, also known as the Knuth shuffle, is a popular algorithm used to randomize the order of elements in an array. This algorithm efficiently generates a uniformly random permutation of the input array. The Fisher-Yates shuffle ensures that each permutation is equally likely, providing a uniform distribution of possible outcomes. Fisher-Yates shuffle is widely used in applications such as creating random games, shuffling decks of cards, and generating random test cases.

Where possible, I have provided the means to shuffle elements in-place without requiring additional memory (for performance focused languages like C, C++, and C#, which are normally used in game engines like Unreal Engine or Unity). In other languages that aren't so performance critical, I've provided the means to simply copy the array and then shuffle the copy instead.

In implementations where we can use Tuple Deconstruction, the time complexity is O(n), where n is the size of the array, as each element is visited and swapped once. Some languages do not support this, and usage (or non-usage) of this technique has been documented in comments where required. Where it cannot be used, we clone the variable to be swapped before assigning iot to its new index, and thus the complexity both specially and temporally increases to O(n+2) as we need to clone and then reassign the variable.

It follows a common set of steps:

  • Start from the end of the array:
  • Initialize an index variable at the last element of the array.
  • Iterate through the array
  • Repeat the following steps until the first element of the array is reached.
    • Generate a random index between 0 and the current index (inclusive).
    • Swap the element at the current index with the element at the randomly generated index.
  • After these iterations, the array elements are shuffled, creating a random permutation.
for n from n - 1 down to 1 do
    k = random integer such that 0 <= k <= n
    swap array[n] and array[k]

For more information on Fisher Yates Algorithm, please see here: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle


About

Fisher Yates Algorithm implementation for randomizing an array of objects, implemented in various languages.

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

License:MIT License


Languages

Language:C++ 25.8%Language:C# 14.7%Language:JavaScript 10.3%Language:Haxe 10.0%Language:TypeScript 8.6%Language:C 7.1%Language:Java 6.8%Language:Python 6.2%Language:Ruby 5.6%Language:Lua 4.9%