python set
Code\n\nclass Solution:\n    # use a set to record the index already sampled \n    # time O(MN) in worst case, we have to sample M*N-1 times to find a spare ind
# Code\n```\nclass Solution:\n    # use a set to record the index already sampled \n    # time O(MN) in worst case, we have to sample M*N-1 times to find a spare index i, j  \n    # space O(MN) in worst case we have M*N elements in seen set \n\n    def __init__(self, m: int, n: int):\n        self.seen = set() \n        self.m = m \n        self.n = n\n    def flip(self) -> List[int]:\n        idx = random.randint(0, self.m*self.n-1) \n        m = idx // self.n\n        n = idx % self.n\n        if (m, n) in self.seen: \n            return self.flip() \n        else: \n            self.seen.add((m,n))\n            return [m, n]\n\n    def reset(self) -> None:\n        self.seen = set() \n\n\n# Your Solution object will be instantiated and called as such:\n# obj = Solution(m, n)\n# param_1 = obj.flip()\n# obj.reset()\n```
['Python3']