Skip to content

Instantly share code, notes, and snippets.

@garaemon
Created October 20, 2012 11:03
Show Gist options
  • Save garaemon/3922990 to your computer and use it in GitHub Desktop.
Save garaemon/3922990 to your computer and use it in GitHub Desktop.
ticket selector
(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