2009년 3월 31일 화요일

[Common Lisp] 로또(Lotto) 번호 생성기

먹고 살기 점점 힘들어져서 이래저래 한숨만 나오는 요즘입니다. 그래서, 혹시나 하는 기대로 로또를 해보기도 하는데, 이건 그때 쓸려고 Common Lisp으로 만든 로또 번호 생성 장난감 코드입니다. 번호를 하나씩 뽑을 때마다 난수 발생 방식으로 수 배열을 섞어줍니다.
(defun random-comp (a b)
(if (= (random 2) 1) t nil))

(defun take-random (lst n mixcnt)
(cond ((null lst) ())
((<= n 0) ())
(t
(let ((nlst
(dotimes (cnt mixcnt lst)
(setq lst (sort lst #'random-comp)))))
(cons (car nlst) (take-random (cdr nlst) (1- n) mixcnt))))))

(defun gen-lotto-seq (mixcnt)
(let ((lst (loop for i from 1 to 45 collect i)))
(take-random lst 6 mixcnt)))

(defun gen-lotto-helper (try mixcnt)
(if (<= try 0)
nil
(cons
(let ((lst (gen-lotto-seq mixcnt)))
(print lst)
(print (apply #'+ lst))
(finish-output)
(sort lst #'<))
(gen-lotto-helper (1- try) mixcnt))))

(defun gen-lotto (try &optional (mixcnt 1000))
(setq *random-state* (make-random-state t))
(gen-lotto-helper try mixcnt))

실행 방법)
(gen-lotto <시도할 게임 수 번호> <하나를 뽑을 때마다 수열을 섞는 회수, 기본값 1000>)

예)
(gen-lotto 5 500) ; 총 5 게임을 시도하고, 각 번호를 뽑을 때마다 500 번씩 섞음
(gen-lotto 3) ; 총 3 게임을 시도하고, 각 번호를 뽑을 때마다 1000 번씩 섞음


아래는 실제로 실행해본 스크린샷입니다.



주의.
이 코드로 인해 발생할 수 있는 정신적 스트레스 및 물질적 궁핍 또는 결여에 대해 저는 어떠한 책임도 지지 않습니다. 단, 이 코드로 인해 발생한 물질적 형태의 이익에 대해 사례를 하시겠다면 마다 하지는 않겠습니다. ;-)