Created
March 7, 2015 16:02
-
-
Save cobalamin/da0acf1e70b789f1b2b2 to your computer and use it in GitHub Desktop.
A little bit of LCG analysis fun in Clojure
This file contains 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
(defn rng [conf] | |
(let [{:keys [a b m]} conf] | |
(fn [x] | |
(mod (+ (* a x) b) m)))) | |
(defn recurredly [f seed] | |
(lazy-seq | |
(let [res (f seed)] | |
(cons res | |
(recurredly f res))))) | |
(defn has-deadlock-at? [conf x] | |
(let [rand-seq (recurredly (rng conf) x) | |
max-count (:m conf)] | |
(loop [n 0, prev x] | |
(let [curr (nth rand-seq n)] | |
(if (> n max-count) | |
nil | |
(if (= curr prev) | |
curr | |
(recur (inc n) curr))))))) | |
(defn- all-deadlocks [conf] | |
(map #(when (has-deadlock-at? conf %) %) | |
(range (:m conf)))) | |
(defn deadlocks [conf] | |
(filter identity (all-deadlocks conf))) | |
(defn has-any-deadlock? [conf] | |
(some identity (all-deadlocks conf))) | |
(comment | |
(deadlocks {:a 5 :b 3 :m 7}) | |
(has-any-deadlock? {:a 5 :b 3 :m 139}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment