Skip to content

Instantly share code, notes, and snippets.

Last active May 25, 2018
What would you like to do?
Reservoir sampling over a list in Common Lisp
(defun sample-list (items &optional (size 1))
(let ((sample (make-array size :fill-pointer 0)))
(loop for item in items
for enum from 1
if (<= enum size)
do (vector-push item sample)
do (let ((rand (random enum)))
(when (< rand size)
(setf (aref sample rand) item)))
finally (return sample))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment