If you want several samples without replacement, shuffle is useful
If you want several samples with replacement, convert to a vector, then (vector-ref v (random (vector-length v))) can be good enough. If you require strong statistical guarantees, i think the math collection has some tools
And take
This is simpler but less efficient than doing proper reservoir sampling as sorawee suggests
Thanks :blush:
use random-ref, it works with any sequence
there’s also random-sample if you’re selecting multiple elements
Wow, didn’t know about random-ref before!
Just peeked at the source code. It’s doing reservoir sampling.
me neither, although it’s been there for a while (6.4)!
Very nice, Bogdan! Thanks for the demonstration! I appreciate that!
Ugh, I think I implemented reservoir sampling at least once without knowing it was there.
Ditto! Will a search for “reservoir” in the docs find random-ref ?
I think there’s a way to add some magic to the docs for random-sample / random-ref so it shows up when reservoir is searched