1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
2 (in-package #:clnl-random)
13 SET-SEED sets the seed on the RNG."
14 (setf mt19937:*random-state* (funcall
15 (symbol-function (intern "MAKE-RANDOM-OBJECT" :mt19937))
16 :state (mt19937:init-random-state n))))
23 N: An integer representing the upper bound
28 NEXT-INT returns the next randomly generated integer.
30 It does so in a way that's in accordance with java.util.Random and
31 the MerseinneTwisterFast that's in NetLogo. It also advances the
32 RNG and is bounded by N."
34 (= n (logand n (- n)))
35 (ash (* n (ash (mt19937:random-chunk mt19937:*random-state*) -1)) -31)
36 (rem (ash (mt19937:random-chunk mt19937:*random-state*) -1) n)))
43 N: A long representing the upper bound
48 NEXT-LONG returns the next randomly generated long.
50 It does so in a way that's in accordance with java.util.Random and
51 the MerseinneTwisterFast that's in NetLogo. It also advances the
52 RNG and is bounded by N."
54 ((unsigned-to-signed (value size) ; We need this because MersenneTwisterFast
55 (if (logbitp (1- size) value) (dpb value (byte size 0) -1) value))
56 (signed-to-unsigned (value size) (ldb (byte size 0) value)))
58 ((y (unsigned-to-signed (mt19937:random-chunk mt19937:*random-state*) 32))
59 (z (unsigned-to-signed (mt19937:random-chunk mt19937:*random-state*) 32)))
60 ;(mod (+ (ash y 32) z) n)))
61 (mod (signed-to-unsigned (ash (+ (ash y 32) z) -1) 63) n))))
63 (defun next-double (&optional (n 1d0))
64 "NEXT-DOUBLE &optional N => DOUBLE
68 N: A double representing the upper bound
73 NEXT-DOUBLE returns the next randomly generated double.
75 It does so in a way that's in accordance with java.util.Random and
76 the MerseinneTwisterFast that's in NetLogo. It also advances the
77 RNG and is bounded by N."
79 ((y (mt19937:random-chunk mt19937:*random-state*))
80 (z (mt19937:random-chunk mt19937:*random-state*)))
83 (+ (ash (ash y -6) 27) (ash z -5))
84 (coerce (ash 1 53) 'double-float))
87 ; Oh, export world, you WILL be mine
89 "EXPORT => RANDOM-STATE
93 RANDOM-STATE: A dump of the current random state
97 EXPORT dumps out the random state to be export world ready.
99 When NetLogo dumps out the current state of the engine, the state of the
100 RNG also gets dumped out so that it can be reinitialized later. This
103 This isn't really useful for regular use."
108 (lambda (x) (if (logbitp (1- 32) x) (dpb x (byte 32 0) -1) x))
109 (funcall (symbol-function (intern "RANDOM-STATE-STATE" :mt19937)) mt19937:*random-state*))))
110 (format nil "0 ~A ~A ~A 0.0 false ~{~A~^ ~}"
111 (first state) (second state) (third state)