Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created July 27, 2017 19:04
Show Gist options
  • Save tmountain/bfdfec526b47a4193c931ad0425409a9 to your computer and use it in GitHub Desktop.
Save tmountain/bfdfec526b47a4193c931ad0425409a9 to your computer and use it in GitHub Desktop.
(defn birthday-collision?
"Generates a birthday (0-364) for person-count
number of people. Returns a boolean value
indicating whether there were two or more
people with the same birthday."
[person-count]
(let [rand-days
(for [x (range person-count)]
(rand-int 365))]
(let [matches (->> (frequencies rand-days)
(vals)
(filter #(> % 1)))]
(not (empty? matches)))))
(defn run-simulation
"Runs the birthday-collision? function
iters number of iterations for person-count
number of people. Prints a formatted result
detailing the result of the simulation."
[iters person-count]
(let [results (for [_ (range iters)] (birthday-collision? person-count))
yes (count (filter true? results))
pct-yes (float (* 100 (/ yes iters)))]
(format "person-count=%d yes %.5f" person-count pct-yes)))
(doseq [n [1 5 10 20 23 30 40 50 60 70]]
(println (run-simulation 10000 n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment