Skip to content

Instantly share code, notes, and snippets.

@ydm
Last active August 29, 2015 13:57
Show Gist options
  • Save ydm/9543627 to your computer and use it in GitHub Desktop.
Save ydm/9543627 to your computer and use it in GitHub Desktop.
;; http://forums.bgdev.org/index.php?showtopic=44083
(defvar ace (/ 1 13))
(defvar heart0 (/ 13 51))
(defvar heart1 (/ 12 51))
(defvar heart (/ (+ heart1 (* 3 heart0)) 4))
(defvar probability (* ace heart))
;; Our deck of cards:
;; 0-12 -- spades (2 3 4 5 6 7 8 9 10 jack queen king ace)
;; 13-25 -- hearts
;; 26-38 -- diamonds
;; 39-51 -- clubs
(defun ace? (n)
(member n '(12 25 38 51)))
(defun heart? (n)
(and (<= 13 n) (<= n 25)))
(setq *random-state* (make-random-state t))
(defun draw-card (&optional selected)
(let ((r (random 52)))
(if (eql r selected)
(draw-card selected)
r)))
(defun draw-and-check ()
(let* ((first (draw-card))
(second (draw-card first)))
(and (ace? first) (heart? second))))
(defun run (runs)
(let ((wins 0))
(loop for i from 1 to runs do
(setq wins (+ wins (if (draw-and-check) 1 0))))
(/ wins runs)))
(progn
(princ "Expected: ")
(princ (float (* 100 probability)))
(fresh-line)
(princ "Actual (10M runs): ")
(princ (float (* 100 (run 10000000))))
(fresh-line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment