This question evaluates understanding of random sampling algorithms, probability distributions (uniform and weighted), handling of weights and edge-case validation, and engineering trade-offs in data-structure-aware implementation for both integer ranges and explicit collections.
Implement a function similar to numpy.random.choice.
choice(a, size=None, replace=True, p=None) -> samples
a
can be:
n
meaning sample from
[0, 1, ..., n-1]
, or
size
can be:
None
(return a single item), or
k
(return a list of length
k
).
replace
:
True
, sampling is with replacement.
False
, sampling is without replacement (no duplicates). Assume
k <= len(a)
.
p
:
a
.
None
, use uniform distribution.
p
, floating point sum != 1,
size=None
, etc.).