This question evaluates understanding of weighted random sampling, probability distributions, input validation, and algorithmic time and space complexity in the context of implementing a sampling routine.
Implement a simplified version of np.random.choice without calling the built-in function. You are given an array items of length n and an optional probability array p of length n, where p[i] is the probability of returning items[i] and the probabilities sum to 1 within numerical tolerance.
Requirements:
p
is omitted, sample uniformly.
size = k
, returning
k
independent samples with replacement.
Bonus: describe an efficient approach for repeated sampling or for sampling without replacement.