1 ;;;; This implementation of RANDOM is based on the Mersenne Twister random
2 ;;;; number generator "MT19937" due to Matsumoto and Nishimura. See:
3 ;;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
4 ;;;; 623-dimensionally equidistributed uniform pseudorandom number
5 ;;;; generator.", ACM Transactions on Modeling and Computer Simulation,
6 ;;;; Vol. 8, No. 1, January pp.3-30 (1998) DOI:10.1145/272991.272995
7 ;;;; http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
18 (in-package "SB!KERNEL")
21 (defconstant mt19937-n 624)
22 (defconstant mt19937-m 397)
23 (defconstant mt19937-upper-mask #x80000000)
24 (defconstant mt19937-lower-mask #x7FFFFFFF)
25 (defconstant mt19937-a #x9908B0DF)
26 (defconstant mt19937-b #x9D2C5680)
27 (defconstant mt19937-c #xEFC60000)
31 ;;; The state is stored in a (simple-array (unsigned-byte 32) (627))
32 ;;; wrapped in a random-state structure:
34 ;;; 0-1: Constant matrix A. [0, #x9908b0df]
38 (deftype random-state-state () `(simple-array (unsigned-byte 32) (,(+ 3 mt19937-n))))
40 (def!method make-load-form ((random-state random-state) &optional environment)
41 (make-load-form-saving-slots random-state :environment environment))
43 (def!method print-object ((state random-state) stream)
44 (if (and *print-readably* (not *read-eval*))
46 (error 'print-not-readable :object state)
48 :report "Print unreadably."
49 (write state :stream stream :readably nil))
51 :report "Supply an object to be printed instead."
52 :interactive read-unreadable-replacement
53 (write object :stream stream)))
54 (format stream "#S(~S ~S #.~S)"
57 `(make-array ,(+ 3 mt19937-n)
61 ',(coerce (random-state-state state) 'list)))))
63 ;;; Generate and initialize a new random-state array. Index is
64 ;;; initialized to 1 and the states to 32bit integers excluding zero.
66 ;;; Seed - A 32bit number.
68 ;;; See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
69 ;;; In the previous versions, MSBs of the seed affect only MSBs of the array.
70 (defun init-random-state (&optional (seed 5489) state)
71 (declare (type (unsigned-byte 32) seed))
72 (let ((state (or state (make-array 627 :element-type '(unsigned-byte 32)))))
73 (check-type state random-state-state)
74 (setf (aref state 0) 0)
75 (setf (aref state 1) mt19937-a)
76 (setf (aref state 2) mt19937-n)
77 (loop for i below mt19937-n
82 (logxor s (ash s -30)))
84 do (setf (aref state p) s))
87 (defvar *random-state*)
88 (defun !random-cold-init ()
89 (/show0 "entering !RANDOM-COLD-INIT")
90 (setf *random-state* (%make-random-state))
91 (/show0 "returning from !RANDOM-COLD-INIT"))
93 (defun make-random-state (&optional state)
95 "Make a random state object. The optional STATE argument specifies a seed
96 for deterministic pseudo-random number generation.
98 As per the Common Lisp standard,
99 - If STATE is NIL or not supplied or is NIL, return a copy of the default
101 - If STATE is a random state, return a copy of it.
102 - If STATE is T, return a randomly initialized state (using operating-system
103 provided randomness source where available, otherwise a poor substitute
104 based on internal time and pid)
106 See SB-EXT:SEED-RANDOM-STATE for a SBCL extension to this functionality."
107 (/show0 "entering MAKE-RANDOM-STATE")
108 (check-type state (or boolean random-state))
109 (seed-random-state state))
111 (defun seed-random-state (&optional state)
113 "Make a random state object. The optional STATE argument specifies a seed
114 for deterministic pseudo-random number generation.
116 As per the Common Lisp standard for MAKE-RANDOM-STATE,
117 - If STATE is NIL or not supplied or is NIL, return a copy of the default
119 - If STATE is a random state, return a copy of it.
120 - If STATE is T, return a randomly initialized state (using operating-system
121 provided randomness source where available, otherwise a poor substitute
122 based on internal time and pid)
124 As a supported SBCL extension, we also support receiving as a seed an object
125 of the following types:
126 - (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*))
128 While we support arguments of any size and will mix the provided bits into
129 the random state, it is probably overkill to provide more than 256 bits worth
130 of actual information.
132 This particular SBCL version also accepts an argument of the following type:
133 - (SIMPLE-ARRAY (UNSIGNED-BYTE 32) (*))
135 This particular SBCL version uses the popular MT19937 PRNG algorithm, and its
136 internal state only effectively contains about 19937 bits of information.
137 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
140 ;; Easy standard cases
142 (/show0 "copying *RANDOM-STATE*")
143 (%make-random-state :state (copy-seq (random-state-state *random-state*))))
145 (/show0 "copying the provided RANDOM-STATE")
146 (%make-random-state :state (copy-seq (random-state-state state))))
147 ;; Standard case, less easy: try to randomly initialize a state.
149 (/show0 "getting randomness from the operating system")
152 ;; On unices, we try to read from /dev/urandom and pass the results
153 ;; to our (simple-array (unsigned-byte 32) (*)) processor below.
154 ;; More than 256 bits would provide a false sense of security.
155 ;; If you need more bits than that, you probably also need
156 ;; a better algorithm too.
159 (with-open-file (r "/dev/urandom" :element-type '(unsigned-byte 32)
160 :direction :input :if-does-not-exist :error)
161 (let ((a (make-array '(8) :element-type '(unsigned-byte 32))))
162 (assert (= 8 (read-sequence a r)))
164 ;; When /dev/urandom is not available, we make do with time and pid
165 ;; Thread ID and/or address of a CONS cell would be even better, but...
167 (/show0 "No /dev/urandom, using randomness from time and pid")
168 (+ (get-internal-real-time)
169 #!+unix (ash (sb!unix:unix-getpid) 32))))))
170 ;; For convenience to users, we accept (simple-array (unsigned-byte 8) (*))
171 ;; We just convert it to (simple-array (unsigned-byte 32) (*)) in a
172 ;; completely straightforward way.
173 ;; TODO: probably similarly accept other word sizes.
174 ((simple-array (unsigned-byte 8) (*))
175 (/show0 "getting random seed from byte vector (converting to 32-bit-word vector)")
176 (let* ((l (length state))
178 (r (if (>= l 2496) 0 (mod l 4)))
179 (y (make-array (list m) :element-type '(unsigned-byte 32))))
180 (loop for i from 0 below (- m (if (zerop r) 0 1))
184 (ash (aref state (+ j 1)) 8)
185 (ash (aref state (+ j 2)) 16)
186 (ash (aref state (+ j 3)) 24))))
187 (unless (zerop r) ;; The last word may require special treatment.
188 (let* ((p (1- m)) (q (* 4 p)))
191 (if (< 1 r) (ash (aref state (+ q 1)) 8) 0)
192 (if (= 3 r) (ash (aref state (+ q 2)) 16) 0)))))
193 (seed-random-state y)))
194 ;; Also for convenience, we accept non-negative integers as seeds.
195 ;; Small ones get passed to init-random-state, as before.
197 (/show0 "getting random seed from 32-bit word")
198 (%make-random-state :state (init-random-state state)))
199 ;; Larger ones ones get trivially chopped into an array of (unsigned-byte 32)
201 (/show0 "getting random seed from bignum (converting to 32-bit-word vector)")
202 (loop with l = (ceiling (integer-length state) 32)
203 with s = (make-array (list l) :element-type '(unsigned-byte 32))
206 do (setf (aref s i) (ldb (byte 32 p) state))
207 finally (return (seed-random-state s))))
208 ;; Last but not least, when provided an array of 32-bit words, we truncate
209 ;; it to 19968 bits and mix these into an initial state. We reuse the same
210 ;; method as the authors of the original algorithm. See
211 ;; http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
212 ;; NB: their mt[i] is our (aref s (+ 3 i))
213 ((simple-array (unsigned-byte 32) (*))
214 (/show0 "getting random seed from 32-bit-word vector")
215 (let ((s (init-random-state 19650218))
216 (i 1) (j 0) (l (length state)))
217 (loop for k downfrom (max mt19937-n l) above 0 do
218 (setf (aref s (+ i 3))
220 (+ (logxor (aref s (+ i 3))
222 (logxor (aref s (+ i 2))
223 (ash (aref s (+ i 2)) -30))))
224 (aref state j) j))) ;; non-linear
225 (incf i) (when (>= i mt19937-n) (setf (aref s 3) (aref s (+ 2 mt19937-n)) i 1))
226 (incf j) (when (>= j l) (setf j 0)))
227 (loop for k downfrom (1- mt19937-n) above 0 do
228 (setf (aref s (+ i 3))
230 (- (logxor (aref s (+ i 3))
232 (logxor (aref s (+ i 2))
233 (ash (aref s (+ i 2)) -30))))
235 (incf i) (when (>= i mt19937-n) (setf (aref s 3) (aref s (+ 2 mt19937-n)) i 1)))
236 (setf (aref s 3) #x80000000) ;; MSB is 1; assuring non-zero initial array
237 (%make-random-state :state s)))))
241 ;;; This function generates a 32bit integer between 0 and #xffffffff
243 #!-sb-fluid (declaim (inline random-chunk))
244 ;;; portable implementation
246 (defun random-mt19937-update (state)
247 (declare (type random-state-state state)
248 (optimize (speed 3) (safety 0)))
250 (declare (type (unsigned-byte 32) y))
252 ((>= kk (+ 3 (- mt19937-n mt19937-m))))
253 (declare (type (mod 628) kk))
254 (setf y (logior (logand (aref state kk) mt19937-upper-mask)
255 (logand (aref state (1+ kk)) mt19937-lower-mask)))
256 (setf (aref state kk) (logxor (aref state (+ kk mt19937-m))
257 (ash y -1) (aref state (logand y 1)))))
258 (do ((kk (+ (- mt19937-n mt19937-m) 3) (1+ kk)))
259 ((>= kk (+ (1- mt19937-n) 3)))
260 (declare (type (mod 628) kk))
261 (setf y (logior (logand (aref state kk) mt19937-upper-mask)
262 (logand (aref state (1+ kk)) mt19937-lower-mask)))
263 (setf (aref state kk) (logxor (aref state (+ kk (- mt19937-m mt19937-n)))
264 (ash y -1) (aref state (logand y 1)))))
265 (setf y (logior (logand (aref state (+ 3 (1- mt19937-n)))
267 (logand (aref state 3) mt19937-lower-mask)))
268 (setf (aref state (+ 3 (1- mt19937-n)))
269 (logxor (aref state (+ 3 (1- mt19937-m)))
270 (ash y -1) (aref state (logand y 1)))))
273 (defun random-chunk (state)
274 (declare (type random-state state))
275 (let* ((state (random-state-state state))
277 (declare (type (mod 628) k))
278 (when (= k mt19937-n)
279 (random-mt19937-update state)
281 (setf (aref state 2) (1+ k))
282 (let ((y (aref state (+ 3 k))))
283 (declare (type (unsigned-byte 32) y))
284 (setf y (logxor y (ash y -11)))
285 (setf y (logxor y (ash (logand y (ash mt19937-b -7)) 7)))
286 (setf y (logxor y (ash (logand y (ash mt19937-c -15)) 15)))
287 (setf y (logxor y (ash y -18)))
290 ;;; Using inline VOP support, only available on the x86 so far.
292 ;;; FIXME: It would be nice to have some benchmark numbers on this.
293 ;;; My inclination is to get rid of the nonportable implementation
294 ;;; unless the performance difference is just enormous.
296 (defun random-chunk (state)
297 (declare (type random-state state))
298 (sb!vm::random-mt19937 (random-state-state state)))
300 #!-sb-fluid (declaim (inline big-random-chunk))
301 (defun big-random-chunk (state)
302 (declare (type random-state state))
303 (logand (1- (expt 2 64))
304 (logior (ash (random-chunk state) 32)
305 (random-chunk state))))
307 ;;; Handle the single or double float case of RANDOM. We generate a
308 ;;; float between 0.0 and 1.0 by clobbering the significand of 1.0
309 ;;; with random bits, then subtracting 1.0. This hides the fact that
310 ;;; we have a hidden bit.
311 #!-sb-fluid (declaim (inline %random-single-float %random-double-float))
312 (declaim (ftype (function ((single-float (0f0)) random-state)
314 %random-single-float))
315 (defun %random-single-float (arg state)
316 (declare (type (single-float (0f0)) arg)
317 (type random-state state))
319 (- (make-single-float
320 (dpb (ash (random-chunk state)
321 (- sb!vm:single-float-digits random-chunk-length))
322 sb!vm:single-float-significand-byte
323 (single-float-bits 1.0)))
325 (declaim (ftype (function ((double-float (0d0)) random-state)
327 %random-double-float))
331 (defun %random-double-float (arg state)
332 (declare (type (double-float (0d0)) arg)
333 (type random-state state))
334 (* (float (random-chunk state) 1d0) (/ 1d0 (expt 2 32))))
338 (defun %random-double-float (arg state)
339 (declare (type (double-float (0d0)) arg)
340 (type random-state state))
342 (- (sb!impl::make-double-float
343 (dpb (ash (random-chunk state)
344 (- sb!vm:double-float-digits random-chunk-length 32))
345 sb!vm:double-float-significand-byte
346 (sb!impl::double-float-high-bits 1d0))
347 (random-chunk state))
350 ;;; using a faster inline VOP
352 (defun %random-double-float (arg state)
353 (declare (type (double-float (0d0)) arg)
354 (type random-state state))
355 (let ((state-vector (random-state-state state)))
357 (- (sb!impl::make-double-float
358 (dpb (ash (sb!vm::random-mt19937 state-vector)
359 (- sb!vm:double-float-digits random-chunk-length
361 sb!vm:double-float-significand-byte
362 (sb!impl::double-float-high-bits 1d0))
363 (sb!vm::random-mt19937 state-vector))
369 (defun %random-integer (arg state)
370 (declare (type (integer 1) arg) (type random-state state))
371 (let ((shift (- random-chunk-length random-integer-overlap)))
372 (do ((bits (random-chunk state)
373 (logxor (ash bits shift) (random-chunk state)))
374 (count (+ (integer-length arg)
375 (- random-integer-extra-bits shift))
379 (declare (fixnum count)))))
381 (defun random (arg &optional (state *random-state*))
382 (declare (inline %random-single-float %random-double-float
383 #!+long-float %random-long-float))
385 ((and (fixnump arg) (<= arg random-fixnum-max) (> arg 0))
386 (rem (random-chunk state) arg))
387 ((and (typep arg 'single-float) (> arg 0.0f0))
388 (%random-single-float arg state))
389 ((and (typep arg 'double-float) (> arg 0.0d0))
390 (%random-double-float arg state))
392 ((and (typep arg 'long-float) (> arg 0.0l0))
393 (%random-long-float arg state))
394 ((and (integerp arg) (> arg 0))
395 (%random-integer arg state))
397 (error 'simple-type-error
398 :expected-type '(or (integer 1) (float (0))) :datum arg
399 :format-control "~@<Argument is neither a positive integer nor a ~
400 positive float: ~2I~_~S~:>"
401 :format-arguments (list arg)))))