def sample(arr, n, unique=False): if unique: arr = set(arr) assert len(arr) > n, "Cannot sample uniquely from a small array." if len(arr) == n: return arr if n > len(arr) / 2: res = list(arr) while len(res) > n: del res[random.randint(0, len(res))] else: res = [] arr = list(arr) while len(res) < n: i = random.randint(0, len(arr)) res.append(arr[i]) del arr[i] else: return [arr[i] for i in random.randints(0, len(arr), n)]
def randint(s, e): """ returns one random integer between s and e. Try using @randints in case you need multiple random integer. @randints is more efficient """ return random.randints(s, e, 1)[0]