X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain%2Frandom.lisp;h=6f500a979558a57fdd519d4323878ab43a5e8052;hb=c34fdd7;hp=7fca5a183d4360398f9ac129b2275067b5891da7;hpb=6c59eecb1168aa02dd44efa6f416a77117e968ce;p=clnl diff --git a/src/main/random.lisp b/src/main/random.lisp index 7fca5a1..6f500a9 100644 --- a/src/main/random.lisp +++ b/src/main/random.lisp @@ -1,8 +1,4 @@ -(defpackage #:cl-nl.random - (:use :common-lisp) - (:export :set-seed :next-int :next-double)) - -(in-package #:cl-nl.random) +(in-package #:clnl-random) ; This is a wrapper around the very nice mersenne twister mt19937 to match ; NetLogo's implementation that tries to match how java.util.Random works @@ -11,7 +7,10 @@ (setf mt19937:*random-state* (mt19937::make-random-object :state (mt19937:init-random-state n)))) (defun next-int (n) - (rem (ash (mt19937:random-chunk mt19937:*random-state*) -1) n)) + (if + (= n (logand n (- n) )) + (ash (* n (ash (mt19937:random-chunk mt19937:*random-state*) -1) ) -31) + (rem (ash (mt19937:random-chunk mt19937:*random-state*) -1) n))) (defun next-double (&optional (n 1d0)) (let @@ -22,3 +21,15 @@ (+ (ash (ash y -6) 27) (ash z -5)) (coerce (ash 1 53) 'double-float)) n))) + +; Oh, export world, you WILL be mine +(defun export () + (let + ((state + (map + 'list + (lambda (x) (if (logbitp (1- 32) x) (dpb x (byte 32 0) -1) x)) + (mt19937::random-state-state mt19937:*random-state*)))) + (format nil "0 ~A ~A ~A 0.0 false ~{~A~^ ~}" + (first state) (second state) (third state) + (nthcdr 3 state))))