Given a stream of data, select k items at random.
(The story was making grep work by returning random samples instead of the whole data set (that was too heavy to store in memory)).
import random
= [1, 7, 4, 8, 2, 6, 5, 9]
input_array
= 4
k = len(input_array)
n
= []
output for i in range(k):
output.append(input_array[i])for j in range(k,n):
= random.randint(0, j)
index if index < k:
= input_array[j]
output[index]
print(f"Input array: {input_array}")
print(f"Output array: {output}")