On Fri, Nov 17, 2023 at 5:34 PM Stefan van der Walt <[email protected]>
wrote:
> On Fri, Nov 17, 2023, at 14:28, Stefan van der Walt wrote:
>
> Attached is a script that implements this solution.
>
>
> And the version with set duplicates checking.
>
If you're going to do the set-checking yourself, then you don't need the
unbounded integer support. This is somewhat slower, probably due to the
greedy tuple conversions, but eliminates all of the complexity of
subclassing `Random`:
def sample_indices(shape, size, rng=None):
rng = np.random.default_rng(rng)
ashape = np.array(shape)
seen = set()
while len(seen) < size:
idx = tuple(rng.integers(0, ashape))
seen.add(idx)
return list(seen)
Unfortunately, subclassing from `Random` still doesn't get you the ability
to sample without replacement for arbitrary-sized populations using
`Random`'s own methods for that. `Random.sample(population, k)` requires
`population` to be a `Sequence`, and that restricts you to index-sized
integers (`ssize_t`) (you can try to fake one, but `len()` will balk if it
gets a too-large integer from `__len__`). But `Random.sample` is only just
doing set-checking anyways, so no loss.
--
Robert Kern
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: [email protected]