Last active
September 14, 2022 15:51
-
-
Save serialhex/318d5ce9d2bbf0c4c98d29a814383e60 to your computer and use it in GitHub Desktop.
Generate normal (gaussian) random numbers in lisp!
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
(defun normal-random (mean std-dev) | |
"Normal random numbers, with the given mean & standard deviation." | |
(do* ((rand-u (* 2 (- 0.5 (random 1.0))) (* 2 (- 0.5 (random 1.0)))) | |
(rand-v (* 2 (- 0.5 (random 1.0))) (* 2 (- 0.5 (random 1.0)))) | |
(rand-s (+ (* rand-u rand-u) (* rand-v rand-v)) | |
(+ (* rand-u rand-u) (* rand-v rand-v)))) | |
((not (or (= 0 rand-s) (>= rand-s 1))) | |
(+ mean | |
(* std-dev | |
(* rand-u (sqrt (/ (* -2.0 (log rand-s)) rand-s)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment