tc39-transfer / proposal-random-functions

Proposal to add a Random namespace and several additional convenience functions for using randomness.
https://tc39-transfer.github.io/proposal-random-functions/
MIT License
3 stars 1 forks source link

Sample *without* replacement? #5

Open tabatkins opened 5 months ago

tabatkins commented 5 months ago

Python offers both sampling with replacement (every value is independent) and sampling without replacement (each value can't occur again in the same call). I think these are pretty useful; there's a lot of examples where you need to sample without replacement, like drawing card hands, and writing such a sampling function can be a bit annoying.

Do we want to add such a function? Python's method is

random.sample(items, numSamples, counts=None)

where counts is an optional array, of the same length as items, that lets you manually specify how many times each item should be considered to be in the collection. (So you don't have to manually repeat the item the specified number of times.)

I suspect we'd want to take the counts value as an options bag, like Random.sample(items, num, {counts}).