Skip to content

Instantly share code, notes, and snippets.

@tbl3rd
Last active August 29, 2015 14:17
Show Gist options
  • Save tbl3rd/b03b9fbb467f7a7d18c8 to your computer and use it in GitHub Desktop.
Save tbl3rd/b03b9fbb467f7a7d18c8 to your computer and use it in GitHub Desktop.
Good luck, Kerry!
(ns kerry)
(defn make-rand-x-to-y
"A function that uses rand-x to generate rand-y,
where (rand-n) has the integer range [0, n)."
[x y]
(let [randx (fn [] (rand-int x))
size (* x y)
choices (into [] (take size (cycle (range y))))]
(fn []
(let [seed (reduce + (repeatedly size randx))]
(choices (mod seed size))))))
(defn sample
"Tabulate n samples of actual calls to (make-rand-x-to-y x y)
against expected (rand-int y) results."
[n x y]
(let [rand-x-to-y (make-rand-x-to-y x y)
expect (frequencies (repeatedly n (fn [] (rand-int y))))
actual (frequencies (repeatedly n rand-x-to-y))]
(println (str "For " n " samples"))
(println (str " expect actual"))
(doseq [i (range y)]
(println (str i " : " (expect i) " " (actual i))))))
(sample 999999 5 7)
;;
;; For 999999 samples
;; expect actual
;; 0 : 142779 143293
;; 1 : 142912 143126
;; 2 : 142792 143258
;; 3 : 142844 142307
;; 4 : 143530 142504
;; 5 : 142692 142854
;; 6 : 142450 142657
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment