Skip to content

Commit 64acc43

Browse files
committed
Added Fisher-Yates shuffle in Ruby
1 parent b8586ce commit 64acc43

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def fisher_yates(arr)
2+
m = arr.length
3+
while m != 0
4+
i = rand(m)
5+
m -= 1
6+
arr[i], arr[m] = arr[m], arr[i]
7+
end
8+
9+
return arr
10+
end
11+
12+
# arr = Proc.new { (1..3).to_a }
13+
# d = Hash.new(0)
14+
# 1000000.times { d[fisher_yates(arr.call).join ""] += 1 }
15+
# p d

0 commit comments

Comments
 (0)