1 (in-package #:clnl-random)
12 SET-SEED sets the seed on the RNG."
13 (setf mt19937:*random-state* (funcall
14 (symbol-function (intern "MAKE-RANDOM-OBJECT" :mt19937))
15 :state (mt19937:init-random-state n))))
22 N: An integer representing the upper bound
27 NEXT-INT returns the next randomly generated integer.
29 It does so in a way that's in accordance with java.util.Random and
30 the MerseinneTwisterFast that's in NetLogo. It also advances the
31 RNG and is bounded by N."
33 (= n (logand n (- n)))
34 (ash (* n (ash (mt19937:random-chunk mt19937:*random-state*) -1)) -31)
35 (rem (ash (mt19937:random-chunk mt19937:*random-state*) -1) n)))
42 N: A long representing the upper bound
47 NEXT-LONG returns the next randomly generated long.
49 It does so in a way that's in accordance with java.util.Random and
50 the MerseinneTwisterFast that's in NetLogo. It also advances the
51 RNG and is bounded by N."
53 ((unsigned-to-signed (value size) ; We need this because MersenneTwisterFast
54 (if (logbitp (1- size) value) (dpb value (byte size 0) -1) value))
55 (signed-to-unsigned (value size) (ldb (byte size 0) value)))
57 ((y (unsigned-to-signed (mt19937:random-chunk mt19937:*random-state*) 32))
58 (z (unsigned-to-signed (mt19937:random-chunk mt19937:*random-state*) 32)))
59 ;(mod (+ (ash y 32) z) n)))
60 (mod (signed-to-unsigned (ash (+ (ash y 32) z) -1) 63) n))))
62 (defun next-double (&optional (n 1d0))
63 "NEXT-DOUBLE &optional N => DOUBLE
67 N: A double representing the upper bound
72 NEXT-DOUBLE returns the next randomly generated double.
74 It does so in a way that's in accordance with java.util.Random and
75 the MerseinneTwisterFast that's in NetLogo. It also advances the
76 RNG and is bounded by N."
78 ((y (mt19937:random-chunk mt19937:*random-state*))
79 (z (mt19937:random-chunk mt19937:*random-state*)))
82 (+ (ash (ash y -6) 27) (ash z -5))
83 (coerce (ash 1 53) 'double-float))
86 ; Oh, export world, you WILL be mine
88 "EXPORT => RANDOM-STATE
92 RANDOM-STATE: A dump of the current random state
96 EXPORT dumps out the random state to be export world ready.
98 When NetLogo dumps out the current state of the engine, the state of the
99 RNG also gets dumped out so that it can be reinitialized later. This
102 This isn't really useful for regular use."
107 (lambda (x) (if (logbitp (1- 32) x) (dpb x (byte 32 0) -1) x))
108 (funcall (symbol-function (intern "RANDOM-STATE-STATE" :mt19937)) mt19937:*random-state*))))
109 (format nil "0 ~A ~A ~A 0.0 false ~{~A~^ ~}"
110 (first state) (second state) (third state)