laurent.orseau
2020-10-26 07:10:11

If you want several samples without replacement, shuffle is useful


laurent.orseau
2020-10-26 07:12:49

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


laurent.orseau
2020-10-26 07:15:19

And take


laurent.orseau
2020-10-26 07:21:43

This is simpler but less efficient than doing proper reservoir sampling as sorawee suggests


joshibharathiramana
2020-10-26 07:57:03

Thanks :blush:


notjack
2020-10-26 07:57:45

use random-ref, it works with any sequence


notjack
2020-10-26 07:59:44

there’s also random-sample if you’re selecting multiple elements


sorawee
2020-10-26 08:43:38

Wow, didn’t know about random-ref before!


sorawee
2020-10-26 08:44:00

Just peeked at the source code. It’s doing reservoir sampling.


laurent.orseau
2020-10-26 08:44:50

me neither, although it’s been there for a while (6.4)!


anything
2020-10-26 13:58:56

Very nice, Bogdan! Thanks for the demonstration! I appreciate that!


samdphillips
2020-10-26 14:13:27

Ugh, I think I implemented reservoir sampling at least once without knowing it was there.


soegaard2
2020-10-26 14:14:23

Ditto! Will a search for “reservoir” in the docs find random-ref ?


sorawee
2020-10-26 23:52:34

notjack
2020-10-27 01:56:03

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