Skip to content

Commit 1c45eca

Browse files
committed
Add Fisher-Yates shuffle in Ruby.
1 parent eeae02a commit 1c45eca

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Shuffles an array with Fisher-Yates algorithm
2+
def fisher_yates(arr)
3+
rng = Random.new()
4+
i = arr.length - 1
5+
while i >= 0
6+
j = rng.rand(i + 1)
7+
temp = arr[i]
8+
arr[i] = arr[j]
9+
arr[j] = temp
10+
i -= 1
11+
end
12+
return arr
13+
end
14+
15+
array = [1,2,3,4,5,6]
16+
puts fisher_yates(array)

0 commit comments

Comments
 (0)