1<html>
2<!--
3   Licensed to the Apache Software Foundation (ASF) under one or more
4  contributor license agreements.  See the NOTICE file distributed with
5  this work for additional information regarding copyright ownership.
6  The ASF licenses this file to You under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with
8  the License.  You may obtain a copy of the License at
9
10       http://www.apache.org/licenses/LICENSE-2.0
11
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17  -->
18    <!-- $Revision: 1054186 $ $Date: 2011-01-01 03:28:46 +0100 (sam. 01 janv. 2011) $ -->
19    <body>
20      <p>Random number and random data generators.</p>
21      <p>Commons-math provides a few pseudo random number generators. The top level interface is RandomGenerator.
22      It is implemented by three classes:
23      <ul>
24        <li>{@link org.apache.commons.math.random.JDKRandomGenerator JDKRandomGenerator}
25            that extends the JDK provided generator</li>
26        <li>AbstractRandomGenerator as a helper for users generators</li>
27        <li>BitStreamGenerator which is an abstract class for several generators and
28            which in turn is extended by:
29            <ul>
30              <li>{@link org.apache.commons.math.random.MersenneTwister MersenneTwister}</li>
31              <li>{@link org.apache.commons.math.random.Well512a Well512a}</li>
32              <li>{@link org.apache.commons.math.random.Well1024a Well1024a}</li>
33              <li>{@link org.apache.commons.math.random.Well19937a Well19937a}</li>
34              <li>{@link org.apache.commons.math.random.Well19937c Well19937c}</li>
35              <li>{@link org.apache.commons.math.random.Well44497a Well44497a}</li>
36              <li>{@link org.apache.commons.math.random.Well44497b Well44497b}</li>
37            </ul>
38          </li>
39        </ul>
40      </p>
41
42      <p>
43      The JDK provided generator is a simple one that can be used only for very simple needs.
44      The Mersenne Twister is a fast generator with very good properties well suited for
45      Monte-Carlo simulation. It is equidistributed for generating vectors up to dimension 623
46      and has a huge period: 2<sup>19937</sup> - 1 (which is a Mersenne prime). This generator
47      is described in a paper by Makoto Matsumoto and Takuji Nishimura in 1998: <a
48      href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf">Mersenne Twister:
49      A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator</a>, ACM
50      Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3--30.
51      The WELL generators are a family of generators with period ranging from 2<sup>512</sup> - 1
52      to 2<sup>44497</sup> - 1 (this last one is also a Mersenne prime) with even better properties
53      than Mersenne Twister. These generators are described in a paper by Fran&ccedil;ois Panneton,
54      Pierre L'Ecuyer and Makoto Matsumoto <a
55      href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng.pdf">Improved Long-Period
56      Generators Based on Linear Recurrences Modulo 2</a> ACM Transactions on Mathematical Software,
57      32, 1 (2006). The errata for the paper are in <a
58      href="http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wellrng-errata.txt">wellrng-errata.txt</a>.
59      </p>
60
61      <p>
62      For simple sampling, any of these generators is sufficient. For Monte-Carlo simulations the
63      JDK generator does not have any of the good mathematical properties of the other generators,
64      so it should be avoided. The Mersenne twister and WELL generators have equidistribution properties
65      proven according to their bits pool size which is directly linked to their period (all of them
66      have maximal period, i.e. a generator with size n pool has a period 2<sup>n</sup>-1). They also
67      have equidistribution properties for 32 bits blocks up to s/32 dimension where s is their pool size.
68      So WELL19937c for exemple is equidistributed up to dimension 623 (19937/32). This means a Monte-Carlo
69      simulation generating a vector of n variables at each iteration has some guarantees on the properties
70      of the vector as long as its dimension does not exceed the limit. However, since we use bits from two
71      successive 32 bits generated integers to create one double, this limit is smaller when the variables are
72      of type double. so for Monte-Carlo simulation where less the 16 doubles are generated at each round,
73      WELL1024 may be sufficient. If a larger number of doubles are needed a generator with a larger pool
74      would be useful.
75      </p>
76
77      <p>
78      The WELL generators are more modern then MersenneTwister (the paper describing than has been published
79      in 2006 instead of 1998) and fix some of its (few) drawbacks. If initialization array contains many
80      zero bits, MersenneTwister may take a very long time (several hundreds of thousands of iterations to
81      reach a steady state with a balanced number of zero and one in its bits pool). So the WELL generators
82      are better to <i>escape zeroland</i> as explained by the WELL generators creators. The Well19937a and
83      Well44497a generator are not maximally equidistributed (i.e. there are some dimensions or bits blocks
84      size for which they are not equidistributed). The Well512a, Well1024a, Well19937c and Well44497b are
85      maximally equidistributed for blocks size up to 32 bits (they should behave correctly also for double
86      based on more than 32 bits blocks, but equidistribution is not proven at these blocks sizes).
87      </p>
88
89      <p>
90      The MersenneTwister generator uses a 624 elements integer array, so it consumes less than 2.5 kilobytes.
91      The WELL generators use 6 integer arrays with a size equal to the pool size, so for example the
92      WELL44497b generator uses about 33 kilobytes. This may be important if a very large number of
93      generator instances were used at the same time.
94      </p>
95
96      <p>
97      All generators are quite fast. As an example, here are some comparisons, obtained on a 64 bits JVM on a
98      linux computer with a 2008 processor (AMD phenom Quad 9550 at 2.2 GHz). The generation rate for
99      MersenneTwister was about 27 millions doubles per second (remember we generate two 32 bits integers for
100      each double). Generation rates for other PRNG, relative to MersenneTwister:
101      </p>
102
103      <p>
104        <table border="1" align="center">
105          <tr BGCOLOR="#CCCCFF"><td colspan="2"><font size="+2">Example of performances</font></td></tr>
106          <tr BGCOLOR="#EEEEFF"><font size="+1"><td>Name</td><td>generation rate (relative to MersenneTwister)</td></font></tr>
107          <tr><td>{@link org.apache.commons.math.random.MersenneTwister MersenneTwister}</td><td>1</td></tr>
108          <tr><td>{@link org.apache.commons.math.random.JDKRandomGenerator JDKRandomGenerator}</td><td>between 0.96 and 1.16</td></tr>
109          <tr><td>{@link org.apache.commons.math.random.Well512a Well512a}</td><td>between 0.85 and 0.88</td></tr>
110          <tr><td>{@link org.apache.commons.math.random.Well1024a Well1024a}</td><td>between 0.63 and 0.73</td></tr>
111          <tr><td>{@link org.apache.commons.math.random.Well19937a Well19937a}</td><td>between 0.70 and 0.71</td></tr>
112          <tr><td>{@link org.apache.commons.math.random.Well19937c Well19937c}</td><td>between 0.57 and 0.71</td></tr>
113          <tr><td>{@link org.apache.commons.math.random.Well44497a Well44497a}</td><td>between 0.69 and 0.71</td></tr>
114          <tr><td>{@link org.apache.commons.math.random.Well44497b Well44497b}</td><td>between 0.65 and 0.71</td></tr>
115        </table>
116      </p>
117
118      <p>
119      So for most simulation problems, the better generators like {@link
120      org.apache.commons.math.random.Well19937c Well19937c} and {@link
121      org.apache.commons.math.random.Well44497b Well44497b} are probably very good choices.
122      </p>
123
124      <p>
125      Note that <em>none</em> of these generators are suitable for cryptography. They are devoted
126      to simulation, and to generate very long series with strong properties on the series as a whole
127      (equidistribution, no correlation ...). They do not attempt to create small series but with
128      very strong properties of unpredictability as needed in cryptography.
129      </p>
130
131    </body>
132</html>
133