Skip to content

Instantly share code, notes, and snippets.

@daemianmack
Created September 10, 2012 16:41
Show Gist options
  • Save daemianmack/3692024 to your computer and use it in GitHub Desktop.
Save daemianmack/3692024 to your computer and use it in GitHub Desktop.
Clojure Problem of the Day #9
(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