Skip to content

Instantly share code, notes, and snippets.

@serialhex
Last active September 14, 2022 15:51
Show Gist options
  • Save serialhex/318d5ce9d2bbf0c4c98d29a814383e60 to your computer and use it in GitHub Desktop.
Save serialhex/318d5ce9d2bbf0c4c98d29a814383e60 to your computer and use it in GitHub Desktop.
Generate normal (gaussian) random numbers in lisp!
(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