Created
June 9, 2014 09:13
-
-
Save lispm/c7c7ebb56ef229afc1b5 to your computer and use it in GitHub Desktop.
lottery
This file contains 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
;; here is a version with a better shuffle function. Note that the shuffle | |
;; function is written in a functional Lisp style. You need to read it from | |
;; inside to outside. | |
;; it uses a vector where the elements get a random double float attached. | |
;; The vector gets sorted by the random double floats. | |
;; the vector-iota function is another utility function | |
;; the 'domain' level LOTTERY function then is just a composition of the utility functions | |
(defun shuffle (v) | |
(map-into v | |
#'car | |
(sort (map-into (make-array (length v)) | |
(lambda (e) | |
(cons e (random 1.0d0))) | |
v) | |
#'< | |
:key #'cdr))) | |
(defun vector-iota (start end) | |
"returns a vector with elements from start to end" | |
(check-type start fixnum) | |
(check-type end fixnum) | |
(assert (>= end start) (start end)) | |
(let ((v (make-array (- end start -1)))) | |
(loop for i from start upto end do (setf (aref v (- i start)) i)) | |
v)) | |
(defun lottery (&aux | |
(start 1) | |
(end 49)) | |
(sort (subseq (shuffle (vector-iota start end)) | |
0 6) | |
#'<)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment