Created
September 10, 2012 16:41
-
-
Save daemianmack/3692024 to your computer and use it in GitHub Desktop.
Clojure Problem of the Day #9
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 clj.core) | |
(def nums | |
(range 0 10)) | |
(defn fair [] | |
(rand-nth nums)) | |
(defn unfair [] | |
(let [f1 #(rand-int 4) | |
f2 (fair)] | |
(rand-nth [f1 f2]))) | |
(def expected-occurrence | |
(/ 1 (count nums))) | |
(defn over-tolerance? [actual-occurrence tolerance] | |
(-> (- (double actual-occurrence) expected-occurrence) | |
(Math/abs) | |
(> tolerance))) | |
(defn all-within-tolerance? [tolerance averages] | |
(not-any? #(over-tolerance? % tolerance) averages)) | |
(defn fair-random? [f n tolerance] | |
(let [output(repeatedly n f) | |
freq-vals (map val (frequencies output)) | |
averages(map #(/ % n) freq-vals)] | |
(all-within-tolerance? tolerance averages))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment