Created
January 19, 2014 07:33
-
-
Save regularcoder/8501660 to your computer and use it in GitHub Desktop.
Park-Miller Minimum Standard Random Number Generator in Common Lisp
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
(defun get-msrn-naive (x n) | |
(let ((a 16807) (m 2147483647)) | |
(mod (* | |
a | |
;Multiply either by x or recursive call | |
(if (eq n 1) | |
x | |
(get-msrn-naive x (- n 1)))) | |
m))) | |
(defun get-next-msrn (x) | |
(let ( | |
(a 16807) | |
(m 2147483647) | |
(q 127773) | |
(r 2836) | |
) | |
(let | |
( | |
(result | |
(- | |
(* a (mod x q)) | |
(* r (floor (/ x q))) | |
) | |
) | |
) | |
(if | |
(<= result 0) | |
(+ result m) | |
result | |
) | |
) | |
) | |
) | |
(defun get-msrn-schrage (x n) | |
(if (eq n 1) | |
(get-next-msrn x) | |
(get-msrn-schrage (get-next-msrn x) (- n 1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment