X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?p=clnl;a=blobdiff_plain;f=src%2Fmain%2Frandom.lisp;h=fad9431ff26a4a8040f2df12b481209f77aa978d;hp=d8a38c0a4cbaf400ec3d36150a8a65b949e3d07e;hb=5c8699f151207953f4029e0fc6c488afce99f756;hpb=471de83db1aee70065808cbc061867e3320bf4b7 diff --git a/src/main/random.lisp b/src/main/random.lisp index d8a38c0..fad9431 100644 --- a/src/main/random.lisp +++ b/src/main/random.lisp @@ -1,20 +1,54 @@ (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 - (defun set-seed (n) + "SET-SEED => RESULT + +ARGUMENTS AND VALUES: + + RESULT: undefined + +DESCRIPTION: + + SET-SEED sets the seed on the RNG." (setf mt19937:*random-state* (funcall (symbol-function (intern "MAKE-RANDOM-OBJECT" :mt19937)) :state (mt19937:init-random-state n)))) (defun next-int (n) + "NEXT-INT N => INT + +ARGUMENTS AND VALUES: + + N: An integer representing the upper bound + INT: An integer + +DESCRIPTION: + + NEXT-INTEGER returns the next randomly generated integer. + + It does so in a way that's in accordance with java.util.Random and + the MerseinneTwisterFast that's in NetLogo. It also advances the + RNG and is bounded by 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)) + "NEXT-DOUBLE &optional N => DOUBLE + +ARGUMENTS AND VALUES: + + N: A double representing the upper bound + DOUBLE: A double + +DESCRIPTION: + + NEXT-DOUBLE returns the next randomly generated double. + + It does so in a way that's in accordance with java.util.Random and + the MerseinneTwisterFast that's in NetLogo. It also advances the + RNG and is bounded by N." (let ((y (mt19937:random-chunk mt19937:*random-state*)) (z (mt19937:random-chunk mt19937:*random-state*))) @@ -26,6 +60,21 @@ ; Oh, export world, you WILL be mine (defun export () + "EXPORT => RANDOM-STATE + +ARGUMENTS AND VALUES: + + RANDOM-STATE: A dump of the current random state + +DESCRIPTION: + + EXPORT dumps out the random state to be export world ready. + + When NetLogo dumps out the current state of the engine, the state of the + RNG also gets dumped out so that it can be reinitialized later. This + accomplishes that. + + This isn't really useful for regular use." (let ((state (map