Skip to content

Instantly share code, notes, and snippets.

@omasanori
Created September 29, 2011 05:29
Show Gist options
  • Save omasanori/1250044 to your computer and use it in GitHub Desktop.
Save omasanori/1250044 to your computer and use it in GitHub Desktop.
A hobby implementation of linearcongruential generators.
(defn lcg
"Returns a lazy seq of random numbers using linear congruential
generators (LCGs)."
[seed a b m]
(map #(float (/ %))
(drop 1
(iterate (fn [x] (mod (+ (* a x) b) m))
seed))))
(defn lcg-average
"Returns average of x random numbers using LCGs with seed."
[x seed]
(let [ans (take x (lcg seed 997 1 65536))]
(/ (apply + ans) x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment