LC 0384 [M] Shuffle an Array - ALawliet/algorithms GitHub Wiki

algorithm is called Fisher-Yates shuffle for every index i, pick a random index j from i to end, and swap(i, j)

class Solution:
    def __init__(self, nums):
        self.nums = nums[:]

    def reset(self):
        return self.nums
        
    def shuffle(self):
        self.copy = self.nums[:]
        n = len(self.copy)
        for i in range(n):
            j = randint(i, n - 1)
            self.copy[i], self.copy[j] = self.copy[j], self.copy[i]
        return self.copy