Last active
August 29, 2015 14:17
-
-
Save tbl3rd/b03b9fbb467f7a7d18c8 to your computer and use it in GitHub Desktop.
Good luck, Kerry!
This file contains hidden or 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
(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