Navigation Menu

Skip to content

Commit

Permalink
RANDOM-SELECTION function.
Browse files Browse the repository at this point in the history
  • Loading branch information
gigamonkey committed Mar 24, 2011
1 parent fc704fb commit a3bf346
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages.lisp
Expand Up @@ -41,6 +41,7 @@
:day-name
:parse-date-string
:reverse-translate-zone
:random-selection
:now
:unix-time
:mklist
Expand Down
21 changes: 21 additions & 0 deletions randomization.lisp
Expand Up @@ -25,4 +25,25 @@ Rudiak-Gould in comp.lang.functional, Message-ID:
(push item heads)
(push item tails))))))

(defun random-selection (list n)
(let* ((rest (nthcdr n list))
(sample (coerce (ldiff list rest) 'vector)))
(loop for item in rest
for p from (1+ n)
for r = (random p)
when (< r n) do (setf (aref sample r) item))
(coerce sample 'list)))

(defun test-random (n m iters)
(let ((h (make-hash-table :test #'equal))
(list (loop for i below m collect i)))
(loop repeat iters
for sample = (sort (random-selection list n) #'<)
do (incf (gethash sample h 0)))
(loop for k being the hash-keys of h using (hash-value v) do
(format t "~&~a => ~f" k (* 100d0 (/ v (float iters 0d0)))))))





0 comments on commit a3bf346

Please sign in to comment.