2018-10-19 07:58:21 +00:00
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
"""
|
2020-06-16 08:09:19 +00:00
|
|
|
|
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a
|
|
|
|
|
finite sequence.
|
2018-05-19 11:07:24 +00:00
|
|
|
|
For more details visit
|
|
|
|
|
wikipedia/Fischer-Yates-Shuffle.
|
2018-10-19 07:58:21 +00:00
|
|
|
|
"""
|
2018-05-19 11:07:24 +00:00
|
|
|
|
import random
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
|
def FYshuffle(list):
|
|
|
|
|
for i in range(len(list)):
|
|
|
|
|
a = random.randint(0, len(list) - 1)
|
|
|
|
|
b = random.randint(0, len(list) - 1)
|
|
|
|
|
list[a], list[b] = list[b], list[a]
|
|
|
|
|
return list
|
2018-05-19 11:07:24 +00:00
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
integers = [0, 1, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
strings = ["python", "says", "hello", "!"]
|
|
|
|
|
print("Fisher-Yates Shuffle:")
|
|
|
|
|
print("List", integers, strings)
|
|
|
|
|
print("FY Shuffle", FYshuffle(integers), FYshuffle(strings))
|