root/trunk/random/rng-types.lisp

Revision 26, 20.4 kB (checked in by lhealy, 9 months ago)

Subversion version stamp.

  • Property svn:keywords set to Id
Line 
1;; Random number generation
2;; Liam Healy, Tue Jul 11 2006 - 23:39
3;; Time-stamp: <2008-02-17 12:07:47EST rng-types.lisp>
4;; $Id$
5
6;;; Random number generator types and information functions.
7
8(in-package :gsl)
9
10;;;;****************************************************************************
11;;;; Random number generator types
12;;;;****************************************************************************
13
14;;; needed?
15(cffi:defcstruct random-number-generator-type
16  (name :pointer)
17  (max :unsigned-long)
18  (min :unsigned-long)
19  (size size)
20  (set :pointer)
21  (get :pointer)
22  (get-double :pointer))
23
24;;;;****************************************************************************
25;;;; Auxiliary functions
26;;;;****************************************************************************
27
28(export '(all-random-number-generators))
29
30(defmfun rng-types-setup ()
31  "gsl_rng_types_setup" ()
32  :c-return :pointer
33  :export nil
34  :documentation
35  "A pointer to an array of all the available generator types,
36   terminated by a null pointer. The function should be
37   called once at the start of the program, if needed.
38   Users should call all-random-number-generators.")
39
40(defun all-random-number-generators ()
41  "A list of all random number generator types."
42  (let ((start (rng-types-setup)))
43    (loop for i from 0
44       for ptr
45       = (cffi:mem-ref
46          (cffi:inc-pointer start (* (cffi:foreign-type-size :pointer) i))
47          :pointer)
48       until (cffi:null-pointer-p ptr)
49       collect ptr)))
50
51;;;;****************************************************************************
52;;;; Defining RNGs and default
53;;;;****************************************************************************
54
55(defmacro def-rng-type (lisp-name &optional documentation gsl-name)
56  "Define the random number generator type."
57  (let ((cname
58         (or gsl-name
59             (remove #\*
60                     (substitute
61                      #\_ #\-
62                      (format nil "gsl_rng_~(~a~)" lisp-name))))))
63    `(defmpar ,lisp-name ,cname ,documentation)))
64
65(def-rng-type *default-type*
66    "The default random number generator type,
67     set by environment variables GSL_RNG_TYPE and GSL_RNG_SEED"
68  "gsl_rng_default")
69
70(defmfun rng-environment-setup ()
71  "gsl_rng_env_setup" ()
72  :c-return :pointer
73  :documentation                        ; FDL
74  "Read the environment variables GSL_RNG_TYPE and
75  GSL_RNG_SEED and use their values to set the corresponding
76  library variables *default-type* and *default-seed*")
77
78;;;;****************************************************************************
79;;;; Modern random number generators
80;;;;****************************************************************************
81
82(def-rng-type *cmrg*                    ; FDL
83    "Combined multiple recursive random number generator
84     This is a combined multiple recursive generator by L'Ecuyer.
85     Its sequence is z_n = (x_n - y_n) mod m_1
86     where the two underlying generators x_n and y_n are,
87     x_n = (a_1 x_{n-1} + a_2 x_{n-2} + a_3 x_{n-3}) mod m_1
88     y_n = (b_1 y_{n-1} + b_2 y_{n-2} + b_3 y_{n-3}) mod m_2
89     with coefficients
90     a_1 = 0, a_2 = 63308, a_3 = -183326,
91     b_1 = 86098, b_2 = 0, b_3 = -539608,
92     and moduli
93     m_1 = 2^31 - 1 = 2147483647 and m_2 = 2145483479.
94     The period of this generator is 2^205 (about 10^61).  It uses
95     6 words of state per generator.  For more information see,
96     P. L'Ecuyer, ``Combined Multiple Recursive Random Number
97     Generators'', Operations Research, 44, 5 (1996), 816--822.")
98
99(def-rng-type *gfsr4*                   ; FDL
100    "Four-tap Generalized Feedback Shift Register
101    The *gfsr4* generator is like a lagged-Fibonacci generator, and
102    produces each number as an xor'd sum of four previous values.
103    r_n = r_{n-A} \oplus r_{n-B} \oplus r_{n-C} \oplus r_{n-D}
104    Ziff (ref below) notes that ``it is now widely known'' that two-tap
105    registers (such as R250, which is described below)
106    have serious flaws, the most obvious one being the three-point
107    correlation that comes from the definition of the generator.  Nice
108    mathematical properties can be derived for GFSR's, and numerics bears
109    out the claim that 4-tap GFSR's with appropriately chosen offsets are as
110    random as can be measured, using the author's test.
111
112    This implementation uses the values suggested the example on p392 of
113    Ziff's article: A=471, B=1586, C=6988, D=9689.
114
115    If the offsets are appropriately chosen (such as the one ones in this
116    implementation), then the sequence is said to be maximal; that means
117    that the period is 2^D - 1, where D is the longest lag.
118    (It is one less than 2^D because it is not permitted to have all
119    zeros in the ra array.)  For this implementation with
120    D=9689 that works out to about 10^2917.
121
122    Note that the implementation of this generator using a 32-bit
123    integer amounts to 32 parallel implementations of one-bit
124    generators.  One consequence of this is that the period of this
125    32-bit generator is the same as for the one-bit generator.
126    Moreover, this independence means that all 32-bit patterns are
127    equally likely, and in particular that 0 is an allowed random
128    value.  (We are grateful to Heiko Bauke for clarifying for us these
129    properties of GFSR random number generators.)
130
131    For more information see,
132    Robert M. Ziff, ``Four-tap shift-register-sequence random-number
133    generators'', Computers in Physics, 12(4), Jul/Aug
134    1998, pp 385--392.")
135
136(def-rng-type *mrg*                     ; FDL
137    "Multiple recursive random number generator
138   This is a fifth-order multiple recursive generator by L'Ecuyer, Blouin
139   and Coutre.  Its sequence is
140   x_n = (a_1 x_{n-1} + a_5 x_{n-5}) mod m
141   with a_1 = 107374182, a_2 = a_3 = a_4 = 0, a_5 = 104480 and m = 2^31 - 1.
142   The period of this generator is about 10^46.  It uses 5 words
143   of state per generator.  More information can be found in the following
144   paper,
145    P. L'Ecuyer, F. Blouin, and R. Coutre, ``A search for good multiple
146    recursive random number generators'', ACM Transactions on Modeling and
147    Computer Simulation 3, 87--98 (1993).")
148
149(def-rng-type *mt19937*                 ; FDL
150    "The MT19937 generator of Makoto Matsumoto and Takuji Nishimura is a
151    variant of the twisted generalized feedback shift-register algorithm,
152    and is known as the ``Mersenne Twister'' generator.  It has a Mersenne
153    prime period of 2^19937 - 1 (about 10^6000) and is
154    equi-distributed in 623 dimensions.  It has passed the diehard
155    statistical tests.  It uses 624 words of state per generator and is
156    comparable in speed to the other generators.  The original generator used
157    a default seed of 4357 and choosing s equal to zero in #'rng-set
158    reproduces this.   For more information see,
159    Makoto Matsumoto and Takuji Nishimura, ``Mersenne Twister: A
160    623-dimensionally equidistributed uniform pseudorandom number
161    generator'' ACM Transactions on Modeling and Computer
162    Simulation, Vol. 8, No. 1 (Jan. 1998), Pages 3--30
163    This generator uses the second revision of the
164    seeding procedure published by the two authors above in 2002.  The
165    original seeding procedures could cause spurious artifacts for some seed
166    values. They are still available through the alternative generators")
167                                                                         
168(def-rng-type *mt19937_1999*            ; FDL
169    "Previous version of mt19937.")
170
171(def-rng-type *mt19937_1998*            ; FDL
172    "Previous version of mt19937.")
173
174(def-rng-type *ran0*)
175
176(def-rng-type *ran1*)
177
178(def-rng-type *ran2*)
179
180(def-rng-type *ran3*)
181
182(def-rng-type *ranlux*                  ; FDL
183    "The ranlux generator is an implementation of the original
184    algorithm developed by LÃŒscher.  It uses a
185    lagged-fibonacci-with-skipping algorithm to produce ``luxury random
186    numbers''.  It is a 24-bit generator, originally designed for
187    single-precision IEEE floating point numbers.  This implementation is
188    based on integer arithmetic, while the second-generation versions
189    ranlxs and ranlxd described above provide floating-point
190    implementations which will be faster on many platforms.
191    The period of the generator is about
192    10^171.  The algorithm has mathematically proven properties and
193    it can provide truly decorrelated numbers at a known level of
194    randomness.  The default level of decorrelation recommended by LÃŒscher
195    is provided by this generator, while *ranlux389*
196    gives the highest level of randomness, with all 24 bits decorrelated.
197    Both types of generator use 24 words of state per generator.
198    For more information see,
199    M. LÃŒscher, ``A portable high-quality random number generator for
200    lattice field theory calculations'', Computer Physics
201    Communications, 79 (1994) 100--110.
202    F. James, ``RANLUX: A Fortran implementation of the high-quality
203    pseudo-random number generator of LÃŒscher'', Computer Physics
204    Communications, 79 (1994) 111--114.")
205
206(def-rng-type *ranlux389*)
207
208(def-rng-type *ranlxs0*
209    "This generator is a second-generation version of the
210    *ranlux* algorithm of LÃŒscher, which produces ``luxury random
211    numbers''.  This generator provides single precision output (24 bits) at
212    three luxury levels *ranlxs0*, *ranlxs1* and *ranlxs2*.
213    It uses double-precision floating point arithmetic internally and can be
214    significantly faster than the integer version of *ranlux*,
215    particularly on 64-bit architectures.  The period of the generator is
216    about 10^171.  The algorithm has mathematically proven properties and
217    can provide truly decorrelated numbers at a known level of randomness.
218    The higher luxury levels provide increased decorrelation between samples
219    as an additional safety margin.")
220
221(def-rng-type *ranlxs1*)
222
223(def-rng-type *ranlxs2*)
224
225(def-rng-type *ranlxd1*                 ; FDL
226    "Produce double precision output (48 bits) from the
227    *ranlxs* generator.  The library provides two luxury levels,
228    *ranlxd1* and *ranlxd2*.")
229
230(def-rng-type *ranlxd2*                 ; FDL
231    "Produce double precision output (48 bits) from the
232    *ranlxs* generator.  The library provides two luxury levels,
233    *ranlxd1* and *ranlxd2*.")
234
235(def-rng-type *taus*                    ; FDL
236    "Tausworthe random number generator
237     This is a maximally equidistributed combined Tausworthe generator by
238     L'Ecuyer.  The sequence is x_n = (s^1_n \oplus s^2_n \oplus s^3_n)
239     s1_{n+1} = (((s1_n 4294967294)<<12)^^(((s1_n<<13)^^s1_n)>>19))
240     s2_{n+1} = (((s2_n 4294967288)<< 4)^^(((s2_n<< 2)^^s2_n)>>25))
241     s3_{n+1} = (((s3_n 4294967280)<<17)^^(((s3_n<< 3)^^s3_n)>>11))
242     computed modulo 2^32.  In the formulas above ^^}
243     denotes ``exclusive-or''.  Note that the algorithm relies on the properties
244     of 32-bit unsigned integers and has been implemented using a bitmask
245     of 0xFFFFFFFF to make it work on 64 bit machines.
246     The period of this generator is 2^88 (about
247     10^26).  It uses 3 words of state per generator.  For more
248     information see,
249     P. L'Ecuyer, ``Maximally Equidistributed Combined Tausworthe
250     Generators'', Mathematics of Computation, 65, 213 (1996), 203--213.")
251
252(def-rng-type *taus2*                   ; FDL
253    "The same algorithm as *taus* but with an improved seeding procedure
254     described in the paper,
255     P. L'Ecuyer, ``Tables of Maximally Equidistributed Combined LFSR
256     Generators'', Mathematics of Computation, 68, 225 (1999), 261--269
257     The generator *taus2* should now be used in preference to *taus*.")
258
259(def-rng-type *taus113*)
260
261;;;;****************************************************************************
262;;;; UNIX random number generators
263;;;;****************************************************************************
264
265(def-rng-type *rand*                    ; FDL
266    "The BSD rand() generator.  Its sequence is
267   x_{n+1} = (a x_n + c) mod m with a = 1103515245,
268   c = 12345 and m = 2^31.
269   The seed specifies the initial value,  x_1.
270   The period of this generator is 2^31, and it uses
271   1 word of storage per generator.")
272
273(def-rng-type *rand48*                  ; FDL
274    "The Unix rand48 generator.  Its sequence is
275     x_{n+1} = (a x_n + c) mod m
276     defined on 48-bit unsigned integers with
277     a = 25214903917, c = 11 and m = 2^48.
278     The seed specifies the upper 32 bits of the initial value, x_1,
279     with the lower 16 bits set to 0x330E.  The function
280     #'get-random-number returns the upper 32 bits from each term of the
281     sequence.  This does not have a direct parallel in the original
282     rand48 functions, but forcing the result to type long int
283     reproduces the output of mrand48.  The function
284     #'uniform uses the full 48 bits of internal state to return
285     the double precision number x_n/m, which is equivalent to the
286     function drand48.  Note that some versions of the GNU C Library
287     contained a bug in mrand48 function which caused it to produce
288     different results (only the lower 16-bits of the return value were set).")
289
290(def-rng-type *random128_bsd*)
291(def-rng-type *random128_glibc2*)
292(def-rng-type *random128_libc5*)
293(def-rng-type *random256_bsd*)
294(def-rng-type *random256_glibc2*)
295(def-rng-type *random256_libc5*)
296(def-rng-type *random32_bsd*)
297(def-rng-type *random32_glibc2*)
298(def-rng-type *random32_libc5*)
299(def-rng-type *random64_bsd*)
300(def-rng-type *random64_glibc2*)
301(def-rng-type *random64_libc5*)
302(def-rng-type *random8_bsd*)
303(def-rng-type *random8_glibc2*)
304(def-rng-type *random8_libc5*)
305(def-rng-type *random_bsd*)
306(def-rng-type *random_glibc2*)
307(def-rng-type *random_libc5*)
308
309;;;;****************************************************************************
310;;;; Obsolete random number generators
311;;;;****************************************************************************
312
313;;; FDL
314;;; Other random number generators
315
316;;; The generators in this section are provided for compatibility with
317;;; existing libraries.  If you are converting an existing program to use GSL
318;;; then you can select these generators to check your new implementation
319;;; against the original one, using the same random number generator.  After
320;;; verifying that your new program reproduces the original results you can
321;;; then switch to a higher-quality generator.
322
323;;; Note that most of the generators in this section are based on single
324;;; linear congruence relations, which are the least sophisticated type of
325;;; generator.  In particular, linear congruences have poor properties when
326;;; used with a non-prime modulus, as several of these routines do (e.g.
327;;; with a power of two modulus, 2^31 or 2^32).  This
328;;; leads to periodicity in the least significant bits of each number,
329;;; with only the higher bits having any randomness.  Thus if you want to
330;;; produce a random bitstream it is best to avoid using the least
331;;; significant bits.
332
333(def-rng-type *ranf*                    ; FDL
334    "Obsolete, use only for compatibility.")
335
336(def-rng-type *ranmar*                  ; FDL
337    "Obsolete, use only for compatibility.")
338
339(def-rng-type *r250*                    ; FDL
340    "Obsolete, use only for compatibility.
341     The shift-register generator of Kirkpatrick and Stoll.  The
342     sequence is based on the recurrence
343     x_n = x_{n-103} \oplus x_{n-250} where \oplus
344     denotes ``exclusive-or'', defined on 32-bit words.
345     The period of this generator is about 2^250 and it
346     uses 250 words of state per generator.
347     For more information see,
348     S. Kirkpatrick and E. Stoll, ``A very fast shift-register sequence random
349     number generator'', Journal of Computational Physics}, 40, 517--526
350     (1981)")
351
352(def-rng-type *tt800*                   ; FDL
353    "Obsolete, use only for compatibility.
354     An earlier version of the twisted generalized feedback
355     shift-register generator, and has been superseded by the development of
356     MT19937.  However, it is still an acceptable generator in its own
357     right.  It has a period of 2^800 and uses 33 words of storage
358     per generator.
359     For more information see,
360     Makoto Matsumoto and Yoshiharu Kurita, ``Twisted GFSR Generators
361     II'', ACM Transactions on Modelling and Computer Simulation,
362     Vol.: 4, No.: 3, 1994, pages 254--266.")
363
364;;; The following generators are included only for historical reasons, so
365;;; that you can reproduce results from old programs which might have used
366;;; them.  These generators should not be used for real simulations since
367;;; they have poor statistical properties by modern standards.
368
369(def-rng-type *vax*                     ; FDL
370    "Obsolete, use only for compatibility.")
371
372(def-rng-type *transputer*              ; FDL
373    "Obsolete, use only for compatibility.")
374
375(def-rng-type *randu*                   ; FDL
376    "Obsolete, use only for compatibility.")
377
378(def-rng-type *minstd*                  ;FDL
379    "Obsolete, use only for compatibility.
380    Park and Miller's ``minimal standard'' minstd generator, a
381    simple linear congruence which takes care to avoid the major pitfalls of
382    such algorithms.  Its sequence is x_{n+1} = (a x_n) mod m
383    with a = 16807 and m = 2^31 - 1 = 2147483647.
384    The seed specifies the initial value, x_1.  The period of this
385    generator is about 2^31.
386    This generator is used in the IMSL Library (subroutine RNUN) and in
387    MATLAB (the RAND function).  It is also sometimes known by the acronym
388    ``GGL'' (I'm not sure what that stands for).
389    For more information see
390    Park and Miller, ``Random Number Generators: Good ones are hard to find'',
391    Communications of the ACM, October 1988, Volume 31, No 10, pages
392    1192--1201.")
393
394(def-rng-type *uni*                     ; FDL
395    "Obsolete, use only for compatibility.")
396
397(def-rng-type *uni32*                   ; FDL
398    "Obsolete, use only for compatibility.")
399
400(def-rng-type *slatec*                  ; FDL
401    "Obsolete, use only for compatibility.")
402
403(def-rng-type *zuf*                     ; FDL
404    "Obsolete, use only for compatibility.
405    The ZUFALL lagged Fibonacci series generator of Peterson.  Its
406    sequence is
407        t = u_{n-273} + u_{n-607}
408        u_n  = t - floor(t)
409    The original source code is available from NETLIB.  For more information
410    see
411    W. Petersen, ``Lagged Fibonacci Random Number Generators for the NEC
412    SX-3'', International Journal of High Speed Computing (1994).")
413
414(def-rng-type *borosh13*                ; FDL
415    "Obsolete, use only for compatibility.
416    The Borosh-Niederreiter random number generator. It is taken
417    from Knuth's Seminumerical Algorithms, 3rd Ed., pages
418    106--108. Its sequence is x_{n+1} = (a x_n) mod m
419    with a = 1812433253 and m = 2^32.
420    The seed specifies the initial value, x_1.")
421
422(def-rng-type *coveyou*                 ; FDL
423    "Obsolete, use only for compatibility.
424     The Coveyou random number generator, taken from Knuth's
425     Seminumerical Algorithms, 3rd Ed., Section 3.2.2. Its sequence
426     is x_{n+1} = (x_n (x_n + 1)) mod m with m = 2^32.
427     The seed specifies the initial value, x_1.")
428
429(def-rng-type *fishman18*               ; FDL
430    "Obsolete, use only for compatibility.
431     The Fishman, Moore III random number generator. It is taken from
432     Knuth's Seminumerical Algorithms, 3rd Ed., pages 106--108. Its
433     sequence is x_{n+1} = (a x_n) mod m with a = 62089911 and
434     m = 2^31 - 1.  The seed specifies the initial value,
435     x_1.")
436
437(def-rng-type *fishman20*               ; FDL
438    "Obsolete, use only for compatibility.
439     The Fishman random number generator. It is taken from Knuth's
440     Seminumerical Algorithms, 3rd Ed., page 108. Its sequence is
441     x_{n+1} = (a x_n) mod m with a = 48271 and
442     m = 2^31 - 1.  The seed specifies the initial value,
443     x_1.")
444
445(def-rng-type *fishman2x*               ; FDL
446    "Obsolete, use only for compatibility.
447     The L'Ecuyer--Fishman random number generator. It is taken from
448     Knuth's Seminumerical Algorithms, 3rd Ed., page 108.
449     Its sequence is z_{n+1} = (x_n - y_n) mod m with
450     m = 2^31 - 1.
451     x_n and y_n are given by the fishman20 and lecuyer21 algorithms.
452     The seed specifies the initial value, x_1.")
453
454(def-rng-type *knuthran*                ; FDL
455    "Obsolete, use only for compatibility.
456    A second-order multiple recursive generator described by Knuth
457    in Seminumerical Algorithms, 3rd Ed., Section 3.6.  Knuth
458    provides its C code.")
459
460(def-rng-type *knuthran2*               ; FDL
461    "Obsolete, use only for compatibility.
462     A second-order multiple recursive generator described by Knuth
463     in Seminumerical Algorithms, 3rd Ed., page 108.  Its sequence is
464     x_n = (a_1 x_{n-1} + a_2 x_{n-2}) mod m
465     with a_1 = 271828183, a_2 = 314159269, and
466     m = 2^31 - 1.")
467
468(def-rng-type *lecuyer21*               ; FDL
469    "Obsolete, use only for compatibility.
470     The L'Ecuyer random number generator, taken from Knuth's
471     Seminumerical Algorithms, 3rd Ed., page 106--108.
472     Its sequence is x_{n+1} = (a x_n) mod m
473     with a = 40692 and m = 2^31 - 249.
474     The seed specifies the initial value, x_1.")
475
476(def-rng-type *waterman14*              ; FDL
477    "Obsolete, use only for compatibility.
478     The Waterman random number generator. It is taken from Knuth's
479     Seminumerical Algorithms, 3rd Ed., page 106--108.
480     Its sequence is x_{n+1} = (a x_n) mod m with
481     a = 1566083941 and m = 2^32.
482     The seed specifies the initial value, x_1.")
Note: See TracBrowser for help on using the browser.