From 57837d4a96e1157fe30a7b7458fdd708d0b3d317 Mon Sep 17 00:00:00 2001 From: Pablo Hildo Date: Thu, 4 Oct 2018 22:45:00 -0300 Subject: [PATCH] Added Fisher and Yates Shuffle --- other/fisher_yates.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 other/fisher_yates.rb diff --git a/other/fisher_yates.rb b/other/fisher_yates.rb new file mode 100644 index 0000000..c28d429 --- /dev/null +++ b/other/fisher_yates.rb @@ -0,0 +1,12 @@ +# Fisher and Yates Shuffle is one of the simplest and most popular shuffling algorithm +def fisher_yates_shuffle(array) + n = array.length + while n > 0 + i = rand(n-=1) + array[i], array[n] = array[n], array[i] + end + return array +end + +arr = [1, 2, 40, 30, 20, 15, 323, 12, 3, 4] +puts fisher_yates_shuffle(arr) \ No newline at end of file