Created
December 7, 2013 11:25
-
-
Save igalshilman/7839968 to your computer and use it in GitHub Desktop.
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
| (defprotocol RandomOperations | |
| (uniform [this]) | |
| (exponential [this rate]) | |
| (normal [this avg stddev]) | |
| (geometric [this rate])) | |
| (extend-protocol RandomOperations | |
| java.util.Random | |
| (uniform [this] | |
| (.nextDouble this)) | |
| (exponential [this rate] | |
| (let [u (- 1 (uniform this))] | |
| (/ (Math/log ^double u) (- ^double rate)))) | |
| (normal [this avg stddev] | |
| (+ avg | |
| (* stddev | |
| (.nextGaussian this)))) | |
| (geometric [this rate] | |
| (let [l (Math/exp (- rate))] | |
| (loop [k 1 | |
| p (uniform this)] | |
| (if (< p l) | |
| (dec k) | |
| (recur (inc k) (* p (uniform this))))))) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment