Files
30-seconds-of-code/snippets/shuffle.md
2018-01-21 10:56:40 +05:30

561 B

shuffle

Randomizes the order of the values of an list, returning a new list.

Uses the Fisher-Yates algorithm to reorder the elements of the list.



from copy import deepcopy
from random import randint


def shuffle(arr):
    temp_arr = deepcopy(arr)
    m = len(temp_arr)
    while (m):
        m -= 1
        i = randint(0, m)
        temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
    return temp_arr

foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3]