Created
October 20, 2012 11:03
-
-
Save garaemon/3922990 to your computer and use it in GitHub Desktop.
ticket selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun random-select (users tickets) | |
(if (> (length users) (length tickets)) | |
(error "cannot allocate tickets for the users because of the number of users exceeds the number of tickets") | |
(progn | |
(let ((sorted-tickets (sort tickets #'<))) | |
(let ((enough-tickets (subseq sorted-tickets 0 (length users))) | |
(tickets-result nil)) | |
(dolist (user users) | |
(let* ((random-ticket-index (random (length enough-tickets))) | |
(random-ticket (elt enough-tickets random-ticket-index))) | |
(setf enough-tickets (remove random-ticket enough-tickets)) | |
(push (cons user random-ticket) tickets-result) | |
)) | |
tickets-result) | |
) | |
)) | |
) | |
(random-select '(yusha hitsuji asa chari kumo motoi shinchan) '(19 27 28 29 38 39 40 41 51 52 53 55)) | |
(random-select '(katakana yusha hitsuji asa chari kumo motoi shinchan) '(26 27 28 33 34 35 36 37 41 42 43 44 64)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment