fork download
  1. (defun sample (sequence n &optional (random-state *random-state*))
  2. (check-type n (integer 0 *))
  3. (let* ((*random-state* random-state)
  4. (v (make-array (length sequence) :fill-pointer t
  5. :initial-contents sequence))
  6. (s (loop repeat n
  7. for i = (random (fill-pointer v))
  8. collect (aref v i)
  9. do (rotatef (aref v i)
  10. (aref v (1- (fill-pointer v))))
  11. (setf (fill-pointer v)
  12. (1- (fill-pointer v))))))
  13. (cond ((stringp sequence)
  14. (coerce s 'string))
  15. ((vectorp sequence)
  16. (coerce s 'vector))
  17. (t
  18. s))))
  19.  
  20. (loop with *random-state* = (make-random-state t)
  21. repeat 5
  22. for s = (loop for i from 1 upto 37 collect i)
  23. do (format t "~:A~%" (sort (sample s 7) #'<)))
  24.  
Success #stdin #stdout 0.01s 25460KB
stdin
Standard input is empty
stdout
(14 23 28 29 31 32 34)
(3 19 26 29 33 34 36)
(2 7 11 12 13 20 34)
(3 10 14 17 19 21 25)
(3 8 12 18 24 34 36)