Professional Documents
Culture Documents
1
1 INTRODUCTION 2
where a is the multiplier, c the increment and An integer version of this generator is
m the modulus and x, a, c, m ∈ N (i.e. S is the implemented in the R function runif (see
set of (positive) integers). f is such that RNG). We include in the package the latest
double version, which corrects undesirable
xn = (axn−1 + c) mod m. deficiency. As described on Knuth’s webpage3
Typically, c and m are chosen to be relatively , the previous version of Knuth-TAOCP fails
prime and a such that ∀x ∈ N, ax mod m 6= 0. randomness test if we generate few sequences
The cycle length of linear congruential gener- with several seeds. The cures to this problem
ators will never exceed modulus m, but can is to discard the first 2000 numbers.
maximised with the three following conditions
• increment c is relatively prime to m, 2.1.3 Mersenne-Twister
• a − 1 is a multiple of every prime dividing These two types of generators are in the big
m, family of matrix linear congruential generators
• a−1 is a multiple of 4 when m is a multiple (cf. L’Ecuyer (1990)). But until here,
of 4, no algorithms exploit the binary structure
see Knuth (2002) for a proof. of computers (i.e. use binary operations).
When c = 0, we have the special case In 1994, Matsumoto and Kurita invented
of Park-Miller algorithm or Lehmer algorithm the TT800 generator using binary operations.
(see Park & Miller (1988)). Let us note that But Matsumoto & Nishimura (1998) greatly
the n+jth term can be easily derived from the improved the use of binary operations and
nth term with a puts to aj mod m (still when proposed a new random number generator
c = 0). called Mersenne-Twister.
Finally, we generally use one of the three Matsumoto & Nishimura (1998) work on
types of output function: the finite set N2 = {0, 1}, so a variable x is
represented by a vectors of ω bits (e.g. 32 bits).
• g : N 7→ [0, 1[, and g(x) = m
x
, They use the following linear recurrence for the
• g : N 7→]0, 1], and g(x) = m−1 ,
x
n + ith term:
x+1/2
• g : N 7→]0, 1[, and g(x) = m .
where >> u (resp. << s) denotes a rightshift xi,j = (α1 xi−1,j + · · · + αk xi−k,j ) mod 2
(leftshift) of u (s) bits. At last, we transform
random integers to reals with one of output for all component j.
functions g proposed above. From Panneton et al. (2006), we have the
Details of the order of the successive op- period length of the recurrence reaches the
erations used in the Mersenne-Twister (MT) upper bound 2k − 1 if and only if the polynom
algorithm can be found at the page 7 of χA is a primitive polynomial over N2 .
Matsumoto & Nishimura (1998). However, the The more complex is the matrix A the slower
least, we need to learn and to retain, is all these will be the associated generator. Thus, we
(bitwise) operations can be easily done in many compromise between speed and quality (of
computer languages (e.g in C) ensuring a very equidistribution). If we denote by ψd the set
fast algorithm. of all d-dimensional vectors produced by the
The set of parameters used are generator from all initial states 1 .
If we divide each dimension into 2l2 cells
• (ω, n, m, r) = (32, 624, 397, 31), (i.e. the unit hypercube [0, 1[d is divided
into 2ld cells), the set ψd is said to be
• a = 0×9908B0DF, b = 0×9D2C5680, c = (d, l)-equidistributed if and only if each cell
0 × EF C60000, contains exactly 2k−dl of its points. The
largest dimension for which the set ψd is (d, l)-
• u = 11, l = 18, s = 7 and t = 15.
equidistributed is denoted by dl .
These parameters ensure a good equidistribu- The great advantage of using this definition
tion and a period of 2nω−r − 1 = 219937 − 1. is we are not forced to compute random points
The great advantages of the MT algorithm to know the uniformity of a generator. Indeed,
are a far longer period than any previous thanks to the linear structure of the recurrence
generators (greater than the period of Park & we can express the property of bits of the
Miller (1988) sequence of 232 − 1 or the period current state. From this we define a dimension
of Knuth (2002) around 2129 ), a far better gap for l bits resolution as δl = bk/lc − dl .
equidistribution (since it passed the DieHard An usual measure of uniformity is the sum
test) as well as an very good computation time of dimension gaps
(since it used binary operations and not the
ω
costly real operation modullus). X
∆1 = δl .
MT algorithm is already implemented in
l=1
R (function runif). However the package
randtoolbox provide functions to compute a Panneton et al. (2006) tries to find generators
new version of Mersenne-Twister (the SIMD- with a dimension gap sum ∆1 around zero
oriented Fast Mersenne Twister algorithm) as and a number Z1 of non-zero coefficients in χA
well as the WELL (Well Equidistributed Long- around k/2. Generators with these two charac-
period Linear) generator. teristics are called Well Equidistributed Long-
period Linear generators. As a benchmark,
2.1.4 Well Equidistributed Long- Mersenne Twister algorithm is characterized
period Linear generators with k = 19937, ∆1 = 6750 and Z1 = 135.
The WELL generator is characterized by the
The MT recurrence can be rewritten as
1
The cardinality of ψd is 2k .
2
xi = Axi−1 , with l an integer such that l ≤ bk/dc.
2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 5
0 L 0
for the k + nth term.
where T. are specific matrices, I the identity Thus the MT recursion is entirely character-
matrix and L has ones on its “top left” corner. ized by
The first two lines are not entirely sparse but
h(ω0 , . . . , ωn−1 ) = (ω0 |ω1 )A ⊕ ωm ,
“fill” with T. matrices. All T. ’s matrices are
here to change the state in a very efficient way,
where ωi denotes the ith word integer (i.e.
while the subdiagonal (nearly full of ones) is
horizontal vectors of N2 ).
used to shift the unmodified part of the current
The general recurrence for the SFMT algo-
state to the next one. See Panneton et al.
rithm extends MT recursion to
(2006) for details.
The MT generator can be obtained with h(ω0 , . . . , ωn−1 ) = ω0 A⊕ωm B⊕ωn−2 C⊕ωn−1 D,
special values of T. ’s matrices. Panneton et al.
(2006) proposes a set of parameters, where where A, B, C, D are sparse matrices over N2 ,
they computed dimension gap number ∆1 . ωi are 128-bit integers and the degree of
The full table can be found in Panneton et al. recursion is n = 19937
128 = 156.
(2006), we only sum up parameters for those The matrices A, B, C and D for a word w
implemented in this package in table 1. are defined as follows,
name k N1 ∆1 128
• wA = w << 8 ⊕ w,
WELL512a 512 225 0
32
WELL1024a 1024 407 0 • wB = w >> 11 ⊗c, where c is a 128-bit
WELL19937a 19937 8585 4
WELL44497a 44497 16883 7 constant and ⊗ the bitwise AND operator,
128
• wC = w >> 8,
Table 1: Specific WELL generators 32
• wD = w << 18,
Let us note that for the last two gener- 128
where << denotes a 128-bit operation while
ators a tempering step is possible in order 32
to have maximally equidistributed generator >> a 32-bit operation, i.e. an operation on
(i.e. (d, l)-equidistributed for all d and l). the four 32-bit parts of 128-bit word w.
These generators are implemented in this Hence the transition function of SFMT is
package thanks to the C code of L’Ecuyer and given by
Panneton.
f : (N2ω )n 7→ (N2ω )n
(ω0 , . . . , ωn−1 ) 7→ (ω1 , . . . , ωn−1 , h(ω0 , . . . , ωn−1 )),
2.1.5 SIMD-oriented Fast Mersenne
Twister algorithms where (N2ω )n is the state space.
A decade after the invention of MT, Mat- The selection of recursion and parameters
sumoto & Saito (2008) enhances their algo- was carried out to find a good dimension of
rithm with the computer of today, which have equidistribution for a given a period. This step
Single Instruction Mutiple Data operations is done by studying the characteristic polyno-
letting to work conceptually with 128 bits mial of f . SFMT allow periods of 2p − 1 with
integers. p a (prime) Mersenne exponent1 . Matsumoto
MT and its successor are part of the family 1
a Mersenne exponent is a prime number p such
of multiple-recursive matrix generators since that 2p − 1 is prime. Prime numbers of the form 2p − 1
they verify a multiple recursive equation with have the special designation Mersenne numbers.
2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 6
& Saito (2008) proposes the following set of the sequence (ui )i is to be uniformly dis-
exponents 607, 1279, 2281, 4253, 11213, 19937, tributed in the unit cube I d with the following
44497, 86243, 132049 and 216091. sense:
The advantage of SFMT over MT is the n
computation speed, SFMT is twice faster 1X
∀J ⊂ I d , lim 11J (ui ) = λd (J),
without SIMD operations and nearly fourt n→+∞ n
i=1
times faster with SIMD operations. SFMT
has also a better equidistribution1 and a better where λd stands for the d-dimensional volume
recovery time from zeros-excess states2 . The (i.e. the d-dimensional Lebesgue measure) and
function SFMT provides an interface to the C 11J the indicator function of subset J. The
code of Matsumoto and Saito. problem is that our discrete sequence will never
constitute a “fair” distribution in I d , since
there will always be a small subset with no
2.2 Quasi random generation
points.
Before explaining and detailing quasi random Therefore, we need to consider a more
generation, we must (quickly) explain Monte- flexible definition of uniform distribution of
Carlo3 methods, which have been introduced a sequence. Before introducing the discrep-
in the forties. In this section, we follow the ancy,
Pn we need to define CardE (u1 , . . . , un ) as
approach of Niederreiter (1978). i=1 11E (ui ) the number of points in subset
Let us work on the d-dimensional unit cube E. Then the discrepancy Dn of the n points
I d = [0, 1]d and with a (multivariate) bounded (ui )1≤i≤n in I d is given by
(Lebesgues) integrable function f on I d . Then
we define the Monte Carlo approximation of CardJ (u1 , . . . , un )
Dn = sup − λd (J)
integral of f over I d by J∈J n
n
Z
1X where J denotes the Q family of all subintervals
f (x)dx ≈ f (Xi ), of I d of the form di=1 [ai , bi ]. If we took the
Id n
i=1
family
Qd of all subintervals of I d of the form
where (Xi )1≤i≤n are independent random i=1 [0, bi ], Dn is called the star discrepancy
points from I d . (cf. Niederreiter (1992)).
The strong law of large numbers ensures the Let us note that the Dn discrepancy is
almost surely convergence of the approxima- nothing else than the L∞ -norm over the unit
tion. Furthermore, the expected integration cube of the difference between the empirical
error is bounded by O( √1n ), with the interest- ratio of points (ui )1≤i≤n in a subset J and the
theoretical point number in J. A L2 -norm can
ing fact it does not depend on dimension d.
be defined as well, see Niederreiter (1992) or
Thus Monte Carlo methods have a wide range
Jäckel (2002).
of applications.
The main difference between (pseudo) The integral error is bounded by
Monte-Carlo methods and quasi Monte-Carlo
1 X n Z
methods is that we no longer use random
f (ui ) − f (x)dx ≤ Vd (f )Dn ,
n
d
points (xi )1≤i≤n but deterministic points. Un- i=1 I
like statistical tests, numerical integration
does not rely on true randomness. Let us where Vd (f ) is the d-dimensional Hardy and
note that quasi Monte-Carlo methods date Krause variation4 of f on I d (supposed to be
from the fifties, and have also been used for finite).
interpolation problems and integral equations Actually the integral error bound is the
solving. product of two independent quantities: the
In the following, we consider a sequence variability of function f through Vd (f ) and the
”’Furthermore the convergence condition on regularity of the sequence through Dn . So, we
want to minimize the discrepancy Dn since we
1
See linear algebra arguments of Matsumoto &
4
Nishimura (1998). Interested readers can find the definition page 966
2
states with too many zeros. of Niederreiter (1978). In a sentence, the Hardy and
3
according to wikipedia the name comes from a Krause variation of f is the supremum of sums of d-
famous casino in Monaco. dimensional delta operators applied to function f .
2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 7
generally do not have a choice in the “problem” 2.2.2 Van der Corput sequences
function f .
An example of quasi-random points, which
We will not explain it but this concept can
have a low discrepancy, is the (unidimensional)
be extented to subset J of the unitR cube I d in
Van der Corput sequences.
order to have a similar bound for J f (x)dx.
Let p be a prime number. Every integer n
In the literature, there were many ways can be decomposed in the p basis, i.e. there
to find sequences with small discrepancy, exists some integer k such that
generally called low-discrepancy sequences or
quasi-random points. A first approach tries to k
X
find bounds for these sequences and to search n= aj pj .
the good parameters to reach the lower bound j=1
or to decrease the upper bound. Another
Then, we can define the radical-inverse func-
school tries to exploit regularity of function f
tion of integer n as
to decrease the discrepancy. Sequences coming
from the first school are called quasi-random k
points while those of the second school are
X aj
φp (n) = .
called good lattice points. pj+1
j=1
which leads to the following asymptotic equiv- (φp1 (n), . . . , φpd (n)) ∈ I d ,
alent for Dn :
r ! where p1 , . . . , pd are pairwise relatively prime
log log n bases. The discrepancy of the Halton sequence
Dn = O . log(n)d
n is asymptotically O n .
2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 8
The following Halton theorem gives us bet- n a13 a12 a11 2 a23 a22 a21 φ(a13 ..) φ(a23 ..)
ter discrepancy estimate of finite sequences. 0 000 000 0 0
For any dimension d ≥ 1, there exists an 1 001 001 1/3 1/3
finite sequence of points in I d such that the 2 002 002 2/3 2/3
discrepancy 3 010 012 1/9 7/9
4 011 010 4/9 1/9
log(n)d−1 1
Dn = O . 5 012 011 7/9 4/9
n
6 020 021 2/9 5/9
Therefore, we have a significant guarantee 7 021 022 5/9 8/9
there exists quasi-random points which are 8 022 020 8/9 2/9
outperforming than traditional Monte-Carlo 9 100 100 1/27 1/27
methods. 10 101 101 10/27 10/27
11 102 102 19/27 19/27
2.2.4 Faure sequences 12 110 112 4/27 22/27
13 111 110 12/27 4/27
The Faure sequences is also based on the
14 112 111 22/27 12/27
decomposition of integers into prime-basis but
they have two differences: it uses only one Table 3: Faure first terms
prime number for basis and it permutes vector
elements from one dimension to another. 2.2.5 Sobol sequences
The basis prime number is chosen as the
smallest prime number greater than the dimen- This sub-section is taken from unpublished
sion d, i.e. 3 when d = 2, 5 when d = 3 or 4 work of Diethelm Wuertz.
etc. . . In the Van der Corput sequence, we The Sobol sequence xn = (xn,1 , . . . , xn,d )
decompose integer n into the p-basis: is generated from a set of binary functions
of length ω bits (vi,j with i = 1, . . . , ω and
k
X j = 1, . . . , d). vi,j , generally called direction
n= aj pj . numbers are numbers related to primitive
j=1
(irreducible) polynomials over the field {0, 1}.
Let a1,j be integer aj used for the decom- In order to generate the jth dimension,
position of n. Now we define a recursive we suppose that the primitive polynomial in
permutation of aj : dimension j is
k
X pj (x) = xq + a1 xq−1 + · · · + aq−1 x + 1.
∀2 ≤ D ≤ d, aD,j = Cji aD−1,j mod p,
j=i Then we define the following q-term recurrence
relation on integers (Mi,j )i
where Cji denotes standard combination
j!
i!(j−i)! . Then we take the radical-inversion Mi,j = 2a1 Mi−1,j ⊕ 22 a2 Mi−2,j ⊕ . . .
φp (aD,1 , . . . , aD,k ) defined as ⊕ 2q−1 aq−1 Mi−q+1,j ⊕ 2q aq Mi−q,j ⊕ Mi−q
k
X aj where i > q.
φp (a1 , . . . , ak ) = ,
pj+1 This allow to compute direction numbers as
j=1
which is the same as above for n defined by the vi,j = Mi,j /2i .
aD,i ’s.
Finally the (d-dimensional) Faure sequence This recurrence is initialized by the set of
is defined by arbitrary odd integers v1,j 2ω , . . . , v,j 2q ω, which
are smaller than 2, . . . , 2q respectively. Finally
(φp (a1,1 , . . . , a1,k ), . . . , φp (ad,1 , . . . , ad,k )) ∈ I d . the jth dimension of the nth term of the Sobol
sequence is with
In the bidimensional case, we work in 3-basis,
first terms of the sequence are listed in table xn,j = b1 v1,j ⊕ b2 v2,j ⊕ · · · ⊕ vω,j ,
3.
1
if the sequence has at least two points, cf.
where
Pω−1 bkk’s are the bits of integer n =
Niederreiter (1978). k=0 bk 2 . The requirement is to use a dif-
2
we omit commas for simplicity. ferent primitive polynomial in each dimension.
2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 9
An e?cient variant to implement the generation x − bxc. The infinite sequence ({nα})n≤0 has
of Sobol sequences was proposed by Antonov a bound for its discrepancy
& Saleev (1979). The use of this approach
1 + log n
is demonstrated in Bratley & Fox (1988) and Dn ≤ C .
n
Press et al. (1996).
This family of infinite sequence ({nα})n≤0 is
called the Kronecker sequence.
2.2.6 Scrambled Sobol sequences A special case of the Kronecker sequence is
Randomized QMC methods are the basis for the Torus algorithm where irrational number
error estimation. A generic recipe is the α is a square root of a prime number. The nth
following: Let A1 , . . . , An be a QMC point set term of the d-dimensional Torus algorithm is
and Xi a scrambled version of Ai . Then we defined by
are searching for randomizations which have √ √
({n p1 }, . . . , {n pd }) ∈ I d ,
the following properties:
where (p1 , . . . , pd ) are prime numbers, gener-
• Uniformity: ThePXi makes the approx- ally the first d prime numbers. With the
imator Iˆ = N1 R Ni=1 f (Xi ) an unbiased previous inequality, we can derive an estimate
estimate of I = [0,1]d f (x)dx. of the Torus algorithm discrepancy:
• Equidistribution: The Xi form a point 1 + log n
O .
set with probability 1; i.e. the random- n
ization process has preserved whatever
special properties the underlying point set 2.2.8 Mixed pseudo quasi random se-
had. quences
Sometimes we want to use quasi-random se-
The Sobol sequences can be scrambled by quences as pseudo random ones, i.e. we want
the Owen’s type of scrambling, by the Faure- to keep the good equidistribution of quasi-
Tezuka type of scrambling, and by a combina- random points but without the term-to-term
tion of both. dependence.
The program we have interfaced to R is One way to solve this problem is to use
based on the ACM Algorithm 659 described by pseudo random generation to mix outputs of
Bratley & Fox (1988) and Bratley et al. (1992). a quasi-random sequence. For example in the
Modifications by Hong & Hickernell (2001) case of the Torus sequence, we have repeat for
allow for a randomization of the sequences. 1≤i≤n
Furthermore, in the case of the Sobol sequence
we followed the implementation of Joe & • draw an integer ni from Mersenne-Twister
Kuo (1999) which can handle up to 1111 in {0, . . . , 2ω − 1}
√
dimensions. • then ui = {ni p}
To interface the Fortran routines to the
R environment some modifications had to be 2.2.9 Good lattice points
performed. One important point was to make
In the above methods we do not take into
possible to re-initialize a sequence and to recall
account a better regularity of the integrand
a sequence without renitialization from R. This
function f than to be of bounded variation in
required to remove BLOCKDATA, COMMON
the sense of Hardy and Krause. Good lattice
and SAVE statements from the original code
point sequences try to use the eventual better
and to pass the initialization variables through
regularity of f .
the argument lists of the subroutines, so that
If f is 1-periodic for each variable, then the
these variables can be accessed from R.
approximation with good lattice points is
Z n
2.2.7 Kronecker sequences 1X i
f (x)dx ≈ f g ,
Id n n
i=1
Another kind of low-discrepancy sequence uses
irrational number and fractional part. The where g ∈ Zd is suitable d-dimensional lattice
fractional part of a real x is denoted by {x} = point. To impose f to be 1-periodic may
3 EXAMPLES OF DISTINGUISHING FROM TRULY RANDOM NUMBERS 10
and real valued functions, which represent [1] 0.6009661 0.4376641 0.8211890
these operations, are extremely discontinuous [4] 0.7238392 0.5656766 0.3260606
and such functions also do not typically occur [7] 0.1005321 0.6435467 0.0891730
in simulation problems. Another reason is [10] 0.7305934
that we need to observe quite a long history
of the output to detect the difference from One will quickly note that two calls to
true randomness. For example, for Mersenne congruRand will not produce the same output.
Twister, we need 624 consecutive numbers. This is due to the fact that we use the machine
On the other hand, if we use a cryptographi- time to initiate the sequence at each call. But
cally strong pseudorandom number generator, the user can set the seed with the function
we may avoid distinguishing from truly ran- setSeed:
dom numbers under any known efficient proce- > setSeed(1)
dure. Such generators are typically slower than > congruRand(10)
Mersenne Twister type generators. The factor
of slow down is, for example for AES, about 5. [1] 7.826369e-06 1.315378e-01
However, note that for simulation problems, [3] 7.556053e-01 4.586501e-01
which require intensive computation besides [5] 5.327672e-01 2.189592e-01
the generating random numbers, using slower, [7] 4.704462e-02 6.788647e-01
but better, generator implies only negligible [9] 6.792964e-01 9.346929e-01
slow down of the computation as a whole.
One can follow the evolution of the nth
integer generated with the option echo=TRUE.
4 Description of the random
> setSeed(1)
generation functions
> congruRand(10, echo=TRUE)
In this section, we detail the R functions im-
1 th integer generated : 1
plemented in randtoolbox and give examples.
2 th integer generated : 16807
3 th integer generated : 282475249
4.1 Pseudo random generation 4 th integer generated : 1622650073
5 th integer generated : 984943658
For pseudo random generation, R provides
6 th integer generated : 1144108930
many algorithms through the function runif
7 th integer generated : 470211272
parametrized with .Random.seed. We encour-
8 th integer generated : 101027544
age readers to look in the corresponding help
9 th integer generated : 1457850878
pages for examples and usage of those func-
10 th integer generated : 1458777923
tions. Let us just say runif use the Mersenne-
[1] 7.826369e-06 1.315378e-01
Twister algorithm by default and other gen-
[3] 7.556053e-01 4.586501e-01
erators such as Wichmann-Hill, Marsaglia-
[5] 5.327672e-01 2.189592e-01
Multicarry or Knuth-TAOCP-20021 .
[7] 4.704462e-02 6.788647e-01
[9] 6.792964e-01 9.346929e-01
4.1.1 congruRand
We can check that those integers are the 10
The randtoolbox package provides two
first terms are listed in table 4, coming from
pseudo-random generators functions :
http://www.firstpr.com.au/dsp/rand31/.
congruRand and SFMT. congruRand computes
We can also check around the 10000th
linear congruential generators, see sub-section
term. From the site http://www.firstpr.
2.1.1. By default, it computes the Park &
com.au/dsp/rand31/, we know that 9998th
Miller (1988) sequence, so it needs only the
to 10002th terms of the Park-Miller sequence
observation number argument. If we want to
are 925166085, 1484786315, 1043618065,
generate 10 random numbers, we type
1589873406, 2010798668. The congruRand
> congruRand(10) generates
1
see Wichmann & Hill (1982), Marsaglia (1994) and > setSeed(1614852353)
Knuth (2002) for details. > congruRand(5, echo=TRUE)
4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 12
We must precise that we do not implement To use the different scrambling option, you
the SFMT algorithm, we “just” use the C just to use the scrambling argument: 0 for
code of Matsumoto & Saito (2008). For the (the default) no scrambling, 1 for Owen, 2
moment, we do not fully use the strength of for Faure-Tezuka and 3 for both types of
their code. For example, we do not use block scrambling.
generation and SSE2 SIMD operations.
> sobol(10)
4.2 Quasi-random generation > sobol(10, scramb=3)
[1] 0.500 0.250 0.750 0.125 0.625 [1] 0.41421356 0.82842712 0.24264069
[4] 0.65685425 0.07106781 0.48528137
[1] 0.3750 0.8750 0.0625 0.5625 0.3125
[7] 0.89949494 0.31370850 0.72792206
init argument is also available for other [10] 0.14213562
quasi-RNG functions.
These
√ √ numbers√ are fractional parts of
2, 2 2, 3 2, . . . , see sub-section 2.2.1 for
4.2.2 Sobol sequences
details.
The function sobol implements the Sobol se-
quences with optional sampling (Owen, Faure- > torus(5, use =TRUE)
Tezuka or both type of sampling). This sub-
section also comes from an unpublished work [1] 0.5398231 0.9540367 0.3682502
of Diethelm Wuertz. [4] 0.7824638 0.1966774
4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 14
0.4
0.0 0.2 0.4 0.6 0.8 1.0 In order to see the difference between, we
can plot the empirical autocorrelation function
u
(acf in R), see figure 2.
> par(mfrow = c(2,1))
Sobol (Owen) > acf(torus(10^5))
> acf(torus(10^5, mix=TRUE))
1.0
0.8
Series torus(10^5)
0.6
v
0.4
0.5
ACF
0.2
−0.5
0.0
u Lag
1.0
> plot(SFMT(1000, 2))
> plot(torus(10^3, 2))
0.8
0.6
SFMT
0.4
1.0
0.2
0.8
0.0
0.6
0.4
u
0.2
Sobol (Faure−Tezuka)
0.0
1.0
u
0.6
Torus
v
0.4
1.0
0.2
0.8
0.0
0.6
0.4
u
0.2
Secondly we compare WELL generator with Now we will show how to use low-discrepancy
Faure-Tezuka-scrambled Sobol sequences on sequences to compute a d-dimensional integral
figure 4. defined on the unit hypercube. We want
4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 16
0.00
simulation number
Quadrature (McN&S) 2 0.75 0.07
G&P 2 0.4 0.06 Figure 5: Error function for Vanilla call
Table 6: list of errors
1
created by Wuertz et al. (2007b).
5 RANDOM GENERATION TESTS 17
0.02
SFMT
Torus
is extremely fast. Whereas for SF-Mersenne Park Miller
zero
0.01
gence is very slow.
relative error
0.00
4.4.3 Pricing of a DOC
-0.01
Now, we want to price a barrier option: a
down-out call i.e. an Downward knock-Out
-0.02
Call1 . These kind of options belongs to the 0e+00 2e+04 4e+04 6e+04 8e+04 1e+05
5.1 Test on one sequence of n num- number of lengths. In theory m equals to +∞,
bers but in pratice, it is a large integer. We fix m
to be at least
5.1.1 Goodness of Fit
log(10−1 ) − 2 log(1 − p) − log(n)
Goodness of Fit tests compare the empiri- ,
cal cumulative distribution function (cdf) Fn log(p)
of ui ’s with a specific distribution (U(0, 1)
in order to have lengths whose appearance
here). The most known test are Kolmogorov-
probabilitie is at least 0.1.
Smirnov, Crámer-von Mises and Anderson-
Darling tests. They use different norms to
quantify the difference between the empirical 5.1.3 The order test
cdf Fn and the true cdf FU(0,1) . The order test looks for another kind of
• Kolmogorov-Smirnov statistic is patterns. We test a d-tuple, if its components
are ordered equiprobably. For example with
√
Kn = n sup Fn (x) − FU(0,1) (x) ,
d = 3, we should have an equal number of
x∈R vectors (ui , ui+1 , ui+2 )i such that
• Crámer-von Mises statistic is
• ui < ui+1 < ui+2 ,
•
Z +∞
2
2 ui < ui+2 < ui+1 ,
Wn = n Fn (x) − FU(0,1) (x) dFU(0,1) (x), • ui+1 < ui < ui+2 ,
−∞
• ui+1 < ui+2 < ui ,
• and Anderson-Darling statistic is • ui+2 < ui < ui+1
2 • and ui+1 < ui+2 < ui .
Z +∞ Fn (x) − FU (x) dFU (x)
(0,1) (0,1)
A2n = n . For some d, we have d! possible orderings of
−∞ FU(0,1) (x)(1 − FU(0,1) (x))
coordinates, which have the same probability
1
Those statistics can be evaluated empirically to appear d! . The chi-squared statistic for the
thanks to the sorted sequence of ui ’s. But order test for a sequence (ui )1≤i≤n is just
we will not detail any further those tests,
d! 1 2
since according to L’Ecuyer & Simard (2007) X (nj − m d! )
S= 1 ,
they are not powerful for random generation m d!
j=1
testing.
where nj ’s are the counts for different orders
5.1.2 The gap test and m = nd . Computing d! possible orderings
has an exponential cost, so in practive d is
The gap test investigates for special patterns
small.
in the sequence (ui )1≤i≤n . We take a subset
[l, u] ⊂ [0, 1] and compute the ’gap’ variables
with 5.1.4 The frequency test
1 if l ≤ Ui ≤ u
Gi = The frequency test works on a serie of ordered
0 otherwise.
contiguous integers (J = [i1 , . . . , il ] ∩ Z). If we
The probability p that Gi equals to 1 is just denote by (ni )1≤i≤n the sample number of the
the u − l (the Lebesgue measure of the subset). set I, the expected number of integers equals
The test computes the length of zero gaps. If to j ∈ J is
we denote by nj the number of zero gaps of 1
length j. × n,
il − i1 + 1
The chi-squared statistic of a such test is
given by which is independent of j. From this, we can
compute a chi-squared statistic
m
X (nj − npj )2
S= , l
(Card(ni = ij ) − m)2
npj S=
X
,
j=1
m
j=1
where pj = (1 − p)2 pj is the probability that
the length of gaps equals to j; and m the max where m = nd .
6 DESCRIPTION OF RNG TEST FUNCTIONS 19
5.2.1 The serial test We retrieve the collision test with f (x) =
2
The most intuitive way to split the unit (x−1)+ and the serial test with f (x) = (x−λ)
λ .
hypercube [0, 1]t into k = dt subcubes. It is Plenty of statistics can be derived, for example
achieved by splitting each dimension into d > 1 if we want to test the number of cells with
pieces. The volume (i.e. a probability) of each at least b points, f (x) = 11(x=b) . For other
cell is just k1 . statistics, see L’Ecuyer et al. (2002).
The associated chi-square statistic is defined
as 5.2.4 The poker test
m
X (Nj − λ)2
S= , The poker test is a test where cells of the unit
λ
j=1 cube [0, 1]t do not have the same volume. If
where Nj denotes the counts and λ = n
their we split the unit cube into dt cells, then by
k
expectation. regrouping cells with left hand corner having
the same number of distinct coordinates we get
the poker test. In a more intuitive way, let
5.2.2 The collision test
us consider a hand of k cards from k different
The philosophy is still the same: we want to cards. The probability to have exactly c
detect some pathological behavior on the unit different cards is
hypercube [0, 1]t . A collision is defined as when
1 k! c
a point vi = (ui , . . . , ui+t−1 ) falls in a cell P (C = c) = 2S ,
where there are already points vj ’s. Let us k (k − c)! k
k
1 143 125
15
2 68 62
3 21 31
4 17 16
Frequency
10
5 6 7.8
6 3 3.9
7 1 2
5
8 2 0.98
9 0 0.49
10 0 0.24
0
11 0 0.12
0.0 0.2 0.4 0.6 0.8 1.0
Order test
2
Note that the Normal approximation is not yet However, even for simulation purposes, con-
implemented and those two approximations sidering the hardness of distinguishing the
are not valid when some expected collision generated numbers from truly random ones is
numbers are below 5. a good measure of the quality of the generator.
In particular, the well-known empirical tests of
6.2.3 The poker test random number generators such as Diehard1
or TestU01 L’Ecuyer & Simard (2007) are
Finally the function poker.test implements based on comparing statistics computed for the
the poker test as described in sub-section 5.2.4. generator with their values expected for truly
We implement for any “card number” denoted random numbers. Consequently, if a generator
by k. A typical example follows fails an empirical test, then the output of the
test provides a way to distinguish the generator
> poker.test(SFMT(10000)) from the truly random numbers.
Besides of general purpose empirical tests
Poker test
constructed without the knowledge of a con-
chisq stat = 0.38, df = 4 crete generator, there are tests specific to a
, p-value = 0.98 given generator, which allow to distinguish
this particular generator from truly random
(sample size : 10000) numbers.
An example of a generator, whose output
observed number 4 192 967 759 78 may easily be distinguished from truly random
numbers, is the older version of Wichmann-Hill
expected number 3.2 192 960 768 77 from 1982. For this generator, we can even
predict the next number in the sequence, if we
6.3 Hardness of detecting a differ- know the last already generated one. Verifying
ence from truly random num- such a predicition is easy and it is, of course,
bers not valid for truly random numbers. Hence,
we can easily distinguish the output of the
Random number generators typically have an generator from truly random numbers. An
internal memory of fixed size, whose content implementation of this test in R derived from
is called the internal state. Since the number McCullough (2008) is as follows.
of possible internal states is finite, the output
sequence is periodic. The length of this > wh.predict <- function(x)
period is an important parametr of the random + {
number generator. For example, Mersenne- + M1 <- 30269
Twister generator, which is the default in R, + M2 <- 30307
has its internal state stored in 624 unsigned + M3 <- 30323
integers of 32 bits each. So, the internal state + y <- round(M1*M2*M3*x)
consists of 19968 bits, but only 19937 are used. + s1 <- y %% M1
The period length is 219937 − 1, which is a + s2 <- y %% M2
Mersenne prime. + s3 <- y %% M3
Large period is not the only important pa- + s1 <- (171*26478*s1) %% M1
rameter of a generator. For a good generator, + s2 <- (172*26070*s2) %% M2
it is not computationally easy to distinguish + s3 <- (170*8037*s3) %% M3
the output of the generator from truly random + (s1/M1 + s2/M2 + s3/M3) %% 1
numbers, if the seed or the index in the + }
sequence is not known. Generators, which > RNGkind("Wichmann-Hill")
are good from this point of view, are used for > xnew <- runif(1)
cryptographic purposes. These generators are > err <- 0
chosen so that there is no known procedure,
1
which could distinguish their output from truly The Marsaglia Random Number CDROM includ-
ing the Diehard Battery of Tests of Randomness, Re-
random numbers within practically available search Sponsored by the National Science Foundation
computation time. For simulations, this (Grants DMS-8807976 and DMS-9206972), copyright
requirement is usually relaxed. 1995 George Marsaglia.
7 CALLING THE FUNCTIONS FROM OTHER PACKAGES 23
L’Ecuyer, P., Simard, R. & Wegenkittl, S. Patterson, T. (1968), ‘The optimum addition
(2002), ‘Sparse serial tests of uniformity for of points to quadrature formulae’, Mathe-
random number generations’, SIAM Journal matics of Computation pp. 847–856. 16
on scientific computing 24(2), 652–668. 19
Planchet, F., Thérond, P. & Jacquemin, J.
Marsaglia, G. (1994), ‘Some portable very- (2005), Modèles Financiers En Assurance,
long-period random number generators’, Economica. 12
Computers in Physics 8, 117–121. 11
Press, W., Teukolsky, W., William, T. &
Brian, P. (1996), Numerical Recipes in
Matsumoto, M. & Nishimura, T. (1998),
Fortran, Cambridge University Press. 9
‘Mersenne twister: A 623-dimensionnally
equidistributed uniform pseudorandom Rubinstein, M. & Reiner, E. (1991), ‘Unscram-
number generator’, ACM Trans. on bling the binary code’, Risk Magazine 4(9).
Modelling and Computer Simulation 17
8(1), 3–30. 3, 4, 6
Wichmann, B. A. & Hill, I. D. (1982), ‘Al-
Matsumoto, M. & Saito, M. (2008), SIMD- gorithm as 183: An efficient and portable
oriented Fast Mersenne Twister: a 128- pseudo-random number generator’, Applied
bit pseudorandom number generator, Monte Statistics 31, 188–190. 11
Carlo and Quasi-Monte Carlo Methods
2006, Springer. 5, 12, 13 Wuertz, D., many others & see the
SOURCE file (2007a), fExoticOptions:
McCullough, B. D. (2008), ‘Microsoft excel’s Rmetrics - Exotic Option Valuation.
‘not the wichmann–hill’ random number URL: http://www.rmetrics.org 17
generators’, Computational Statistics and Wuertz, D., many others & see the
Data Analysis 52, 4587–4593. 10, 22 SOURCE file (2007b), fOptions: Rmetrics -
Basics of Option Valuation.
Namee, J. M. & Stenger, F. (1967), ‘Con-
URL: http://www.rmetrics.org 16
struction of ful ly symmetric numerical inte-
gration formulas’, Numerical Mathatematics
10, 327–344. 16