__init__.py 352 B

12345678910111213141516
  1. import random
  2. def reservoir_sampling(data, nsamples, prng=None):
  3. if prng is None:
  4. prng = random
  5. result = []
  6. for i, entry in enumerate(data):
  7. if i < nsamples:
  8. result.append(entry)
  9. else:
  10. j = prng.randint(0, i)
  11. if j < nsamples:
  12. result[j] = entry
  13. return result