You are on page 1of 25

A note on random number generation

Christophe Dutang and Diethelm Wuertz

October 29, 2021

1
1 INTRODUCTION 2

“Nothing in Nature is random. . . type algorithms, and low discrepancy se-


a thing appears random only through quences should go directly to the next section.
the incompleteness of our knowledge.”
Spinoza, Ethics I1 . 2.1 Pseudo random generation
At the beginning of the nineties, there was no
state-of-the-art algorithms to generate pseudo
random numbers. And the article of Park
1 Introduction & Miller (1988) entitled Random generators:
good ones are hard to find is a clear proof.
Random simulation has long been a very Despite this fact, most users thought the
popular and well studied field of mathematics. rand function they used was good, because
There exists a wide range of applications in of a short period and a term to term depen-
biology, finance, insurance, physics and many dence. But in 1998, Japenese mathematicians
others. So simulations of random numbers are Matsumoto and Nishimura invents the first
crucial. In this note, we describe the most algorithm whose period (219937 − 1) exceeds
random number algorithms the number of electron spin changes since the
Let us recall the only things, that are creation of the Universe (106000 against 10120 ).
truly random, are the measurement of physical It was a big breakthrough.
phenomena such as thermal noises of semicon- As described in L’Ecuyer (1990), a (pseudo)
ductor chips or radioactive sources2 . random number generator (RNG) is defined by
The only way to simulate some randomness a structure (S, µ, f, U, g) where
on computers are carried out by deterministic • S a finite set of states,
algorithms. Excluding true randomness3 , • µ a probability distribution on S, called
there are two kinds random generation: pseudo the initial distribution,
and quasi random number generators. • a transition function f : S 7→ S,
The package randtoolbox provides R func- • a finite set of output symbols U ,
tions for pseudo and quasi random number • an output function g : S 7→ U .
generations, as well as statistical tests to
quantify the quality of generated random Then the generation of random numbers is as
numbers. follows:

1. generate the initial state (called the seed )


2 Overview of random gener- s0 according to µ and compute u0 = g(s0 ),
2. iterate for i = 1, . . . , si = f (si−1 ) and
ation algorithms ui = g(si ).
In this section, we present first the pseudo Generally, the seed s0 is determined using the
random number generation and second the clock machine, and so the random variates
quasi random number generation. By “random u0 , . . . , un , . . . seems “real” i.i.d. uniform
numbers”, we mean random variates of the random variates. The period of a RNG, a key
uniform U(0, 1) distribution. More complex characteristic, is the smallest integer p ∈ N,
distributions can be generated with uniform such that ∀n ∈ N, sp+n = sn .
variates and rejection or inversion methods.
Pseudo random number generation aims to 2.1.1 Linear congruential generators
seem random whereas quasi random number
generation aims to be deterministic but well There are many families of RNGs : linear con-
equidistributed. gruential, multiple recursive,. . . and “computer
Those familiars with algorithms such as lin- operation” algorithms. Linear congruential
ear congruential generation, Mersenne-Twister generators have a transfer function of the
following type
1
quote taken from Niederreiter (1978).
2
for more details go to http://www.random.org/ f (x) = (ax + c) mod m4 ,
randomness/.
3 4
For true random number generation on R, use the this representation could be easily generalized for
random package of Eddelbuettel (2007). matrix, see L’Ecuyer (1990).
2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 3

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 .

Linear congruential generators are imple- xi+n = xi+m ⊕ (xupp low


i |xi+1 )A,
mented in the R function congruRand.
where n > m are constant integers, xupp i
(respectively xlow i ) means the upper (lower)
2.1.2 Multiple recursive generators
ω − r (r) bits of xi and A a ω × ω matrix
A generalisation of linear congruential gener- of N2 . | is the operator of concatenation, so
ators are multiple recursive generators. They xupp low
i |xi+1 appends the upper ω − r bits of xi
are based on the following recurrences with the lower r bits of xi+1 . After a right
multiplication with the matrix A4 , ⊕ adds the
xn = (a1 xn−1 + · · · + ak xn−k c) mod m, result with xi+m bit to bit modulo two (i.e. ⊕
where k is a fixed integer. Hence the nth term denotes the exclusive-or called xor).
of the sequence depends on the k previous one. Once provided an initial seed x0 , . . . , xn−1 ,
A particular case of this type of generators is Mersenne Twister produces random integers
when in 0, . . . , 2ω − 1. All operations used in the
recurrence are bitwise operations, thus it is a
xn = (xn−37 + xn−100 ) mod 230 , very fast computation compared to modulus
operations used in previous algorithms.
which is a Fibonacci-lagged generator1 . The
period is around 2129 . This generator has 3
go to http://www-cs-faculty.stanford.edu/
been invented by Knuth (2002) and is gen- ~knuth/news02.html#rng.  
erally called “Knuth-TAOCP-2002” or simply 4 0 Iω−1
Matrix A equals to whose right
“Knuth-TAOCP”2 . a
multiplication can be done with a bitwise rightshift
1
see L’Ecuyer (1990). operation and an addition with integer a. See
2
TAOCP stands for The Art Of Computer Pro- the section 2 of Matsumoto & Nishimura (1998) for
gramming, Knuth’s famous book. explanations.
2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 4

To increase the equidistribution, Matsumoto where xk are vectors of N2 and A a transition


& Nishimura (1998) added a tempering step: matrix. The charateristic polynom of A is

yi ← xi+n ⊕ (xi+n >> u), 4


χA (z) = det(A−zI) = z k −α1 z k−1 −· · ·−αk−1 z−αk ,
yi ← yi ⊕ ((yi << s) ⊕ b),
yi ← yi ⊕ ((yi << t) ⊕ c), with coefficients αk ’s in N2 . Those coefficients
yi ← yi ⊕ (yi >> l), are linked with output integers by

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

following A matrix matrix constants. For MT, we have the


  following recurrence
T5,7,0 0 . . .
 T0 0 ... 
  xk+n =
 .. 
 0 I .  
Iω−r 0
 
0 0

, xk A ⊕ xk+1 A ⊕ xk+m .
 

 . .
.. ..  0 0 0 Ir
 
..
  | {z }
 . I 0  h(xk ,xk+1 ,...,xm ,...,xk+n−1 )

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

And finally, the Van der Corput sequence


2.2.1 Quasi-random points and dis- is given by (φp (0), φp (1), . . . , φp (n), . . . ) ∈
crepancy [0, 1[. First terms of those sequence for prime
numbers 2 and 3 are given in table 2.
Until here, we do not give any example
of quasi-random points. In the unidimen- n in p-basis φp (n)
sional case, an easy example of quasi-random n p=2 p=3 p=5 p=2 p=3 p=5
points is the sequence of n terms given by 0 0 0 0 0 0 0
1 3
( 2n , 2n , . . . , 2n−1
2n ). This sequence has a dis- 1 1 1 1 0.5 0.333 0.2
1
crepancy n , see Niederreiter (1978) for details. 2 10 2 2 0.25 0.666 0.4
The problem with this finite sequence is it 3 11 10 3 0.75 0.111 0.6
depends on n. And if we want different points 4 100 11 4 0.125 0.444 0.8
numbers, we need to recompute the whole 5 101 12 10 0.625 0.777 0.04
sequence. In the following, we will on work the 6 110 20 11 0.375 0.222 0.24
first n points of an infinite sequence in order 7 111 21 12 0.875 0.555 0.44
to use previous computation if we increase n. 8 1000 22 13 0.0625 0.888 0.64
Moreover we introduce the notion of discrep-
ancy on a finite sequence (ui )1≤i≤n . In the Table 2: Van der Corput first terms
above example, we are able to calculate exactly
the discrepancy. With infinite sequence, this The big advantage of Van der Corput
is no longer possible. Thus, we will only sequence is that they use p-adic fractions
try to estimate asymptotic equivalents of easily computable on the binary structure of
discrepancy. computers.
The discrepancy of the average sequence of
points is governed by the law of the iterated 2.2.3 Halton sequences
logarithm :
The d-dimensional version of the Van der
√ Corput sequence is known as the Halton
nDn
lim sup √ = 1, sequence. The nth term of the sequence is
n→+∞ log log n
define as

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

seem too brutal. But there exists a method + s2 <- y %% M2


to transform f into a 1-periodic function + s3 <- y %% M3
while preserving regularity and value of the + s1 <- (171*26478*s1) %% M1
integrand (see Niederreiter 1978, page 983). + s2 <- (172*26070*s2) %% M2
We have the following theorem for good + s3 <- (170*8037*s3) %% M3
lattice points. For every dimension d ≥ 2 and + (s1/M1 + s2/M2 + s3/M3) %% 1
integer n ≥ 2, there exists a lattice points + }
g ∈ Zd which coordinates relatively prime > RNGkind("Wichmann-Hill")
to n such that the discrepancy Dn of points > xnew <- runif(1)
{ n1 g}, . . . , { nn g} satisfies > maxerr <- 0
> for (i in 1:1000) {
 d
d 1 7 + xold <- xnew
Ds < + + 2 log m . + xnew <- runif(1)
n 2n 5
+ err <- abs(wh.predict(xold) - xnew)
Numerous studies of good lattice points + maxerr <- max(err, maxerr)
try to find point g which minimizes the + }
discrepancy. Korobov  test g of the following > print(maxerr)
form 1, m, . . . , md−1 with m ∈ N. Bahvalov
tries Fibonnaci numbers (F1 , . . . , Fd ). Other [1] 0
g
studies look directly for the point α = n
1 d
e.g. α = p d+1 , . . . , p d+1 or some cosinus The printed error is 0 on some machines and
less than 5 · 10−16 on other machines. This
functions. We let interested readers to look for
is clearly different from the error obtained for
detailed information in Niederreiter (1978).
truly random numbers, which is close to 1.
The requirement that the output of a
3 Examples of distinguishing random number generator should not be dis-
from truly random numbers tinguishable from the truly random numbers
by a simple computation, is directly related to
For a good generator, it is not computationally the way, how a generator is used. Typically,
easy to distinguish the output of the generator we use the generated numbers as an input
from truly random numbers, if the seed or to a computation and we expect that the
the index in the sequence is not known. In distribution of the output (for different seeds
this section, we present examples of generators, or for different starting indices in the sequence)
whose output may be easily distinguished from is the same as if the input are truly random
truly random numbers. numbers. A failure of this assumption implies,
An example of such a generator is the besides of a wrong result of our simulation,
older version of Wichmann-Hill from 1982. that observing the output of the computation
For this generator, we can even predict the allows to distinguish the output from the
next number in the sequence, if we know the generator from truly random numbers. Hence,
last already generated one. Verifying such we want to use a generator, for which we
a predicition is easy and it is, of course, may expect that the calculations used in the
not valid for truly random numbers. Hence, intended application cannot distinguish its
we can easily distinguish the output of the output from truly random numbers.
generator from truly random numbers. An In this context, it has to be noted that
implementation of this test in R derived from many of the currently used generators for
McCullough (2008) is as follows. simulations can be distinguished from truly
random numbers using the arithmetic mod 2
> wh.predict <- function(x) (XOR operation) applied to individual bits of
+ { the output numbers. This is true for Mersenne
+ M1 <- 30269 Twister, SFMT and also all WELL generators.
+ M2 <- 30307 The basis for tolerating this is based on two
+ M3 <- 30323 facts. First, the arithmetic mod 2 and ex-
+ y <- round(M1*M2*M3*x) tracting individual bits of real numbers is not
+ s1 <- y %% M1 directly used in typical simulation problems
4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 11

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

n xn n xn make comparisons with other algorithms. The


1 16807 6 470211272 Park-Miller RNG should not be viewed as a
2 282475249 7 101027544 “good” random generator.
3 1622650073 8 1457850878 Finally, congruRand function has a dim ar-
4 984943658 9 1458777923 gument to generate dim- dimensional vectors of
5 1144108930 10 2007237709 random numbers. The nth vector is build with
d consecutive numbers of the RNG sequence
Table 4: 10 first integers of Park & Miller (i.e. the nth term is the (un+1 , . . . , un+d )).
(1988) sequence
4.1.2 SFMT
1 th integer generated : 1614852353
2 th integer generated : 925166085 The SF- Mersenne Twister algorithm is de-
3 th integer generated : 1484786315 scribed in sub-section 2.1.5. Usage of
4 th integer generated : 1043618065 SFMT function implementing the SF-Mersenne
5 th integer generated : 1589873406 Twister algorithm is the same. First argument
[1] 0.4308140 0.6914075 0.4859725 n is the number of random variates, second
[4] 0.7403425 0.9363511 argument dim the dimension.

with 1614852353 being the 9997th term of > SFMT(10)


Park-Miller sequence. > SFMT(5, 2) #bi dimensional variates
However, we are not limited to the Park-
[1] 0.30689909 0.02239406 0.58191951
Miller sequence. If we change the modulus,
[4] 0.77273017 0.29990975 0.56337680
the increment and the multiplier, we get other
[7] 0.38049435 0.64907176 0.10387555
random sequences. For example,
[10] 0.02851105
> setSeed(12)
[,1] [,2]
> congruRand(5, mod = 2^8, mult = 25, incr = 16, echo= TRUE)
[1,] 0.6514067 0.1228353
[2,] 0.3338572 0.9324829
1 th integer generated : 12
[3,] 0.1915250 0.8570771
2 th integer generated : 60
[4,] 0.3849693 0.7533920
3 th integer generated : 236
[5,] 0.4548959 0.9527464
4 th integer generated : 28
5 th integer generated : 204 A third argument is mexp for Mersenne
[1] 0.234375 0.921875 0.109375 exponent with possible values (607, 1279, 2281,
[4] 0.796875 0.984375 4253, 11213, 19937, 44497, 86243, 132049 and
216091). Below an example with a period of
Those values are correct according to Planchet 2607 − 1:
et al. 2005, page 119.
Here is a example list of RNGs computable > SFMT(10, mexp = 607)
with congruRand:
[1] 0.29537951 0.35445788 0.45068571
RNG mod mult incr [4] 0.76981625 0.38728427 0.43111529
[7] 0.42976159 0.68780424 0.15129638
Knuth - Lewis 232 1664525 1.01e91
[10] 0.01745407
Lavaux - Jenssens 248 31167285 1
Haynes 264 6.36e172 1 Furthermore, following the advice of Mat-
Marsaglia 232 69069 0 sumoto & Saito (2008) for each exponent below
Park - Miller 231 − 1 16807 0 19937, SFMT uses a different set of parameters3
in order to increase the independence of
Table 5: some linear RNGs
random generated variates between two calls.
Otherwise (for greater exponent than 19937)
One may wonder why we implement such
we use one set of parameters4 .
a short-period algorithm since we know the
3
Mersenne-Twister algorithm. It is provided to this can be avoided with usepset argument to
FALSE.
1 4
1013904223. These parameter sets can be found in the C
2
636412233846793005. function initSFMT in SFMT.c source file.
4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 13

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)

4.2.1 Halton sequences [1] 0.5000 0.7500 0.2500 0.3750 0.8750


The function halton implements both the [6] 0.6250 0.1250 0.1875 0.6875 0.9375
Van Der Corput (unidimensional) and Halton
sequences. The usage is similar to pseudo- [1] 0.08301502 0.40333283 0.79155722
RNG functions [4] 0.90135310 0.29438378 0.22406132
[7] 0.58105056 0.62985137 0.05026817
> halton(10) [10] 0.49558967
> halton(10, 2)
It is easier to see the impact of scrambling
[1] 0.5000 0.2500 0.7500 0.1250 0.6250 by plotting two-dimensional sequence in the
[6] 0.3750 0.8750 0.0625 0.5625 0.3125 unit square. Below we plot the default
Sobol sequence and Sobol scrambled by Owen
[,1] [,2] algorithm, see figure 1.
[1,] 0.5000 0.33333333
[2,] 0.2500 0.66666667 > par(mfrow = c(2,1))
[3,] 0.7500 0.11111111 > plot(sobol(1000, 2))
[4,] 0.1250 0.44444444 > plot(sobol(10^3, 2, scram=1))
[5,] 0.6250 0.77777778
[6,] 0.3750 0.22222222
[7,] 0.8750 0.55555556
4.2.3 Faure sequences
[8,] 0.0625 0.88888889
[9,] 0.5625 0.03703704 In a near future, randtoolbox package will
[10,] 0.3125 0.37037037 have an implementation of Faure sequences.
For the moment, there is no function faure.
You can use the init argument set to
FALSE (default is TRUE) if you want that two
4.2.4 Torus algorithm (or Kronecker
calls to halton functions do not produce the
sequence)
same sequence (but the second call continues
the sequence from the first call. The function torus implements the Torus
algorithm.
> halton(5)
> halton(5, init=FALSE) > torus(10)

[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

a set of prime numbers, e.g. torus(10, 3,


Sobol (no scrambling) c(7, 11, 13)). The dimension argument is
limited to 100 0001 .
1.0

As described in sub-section 2.2.8, one way


to deal with serial dependence is to mix
0.8

the Torus algorithm with a pseudo random


generator. The torus function offers this
0.6

operation thanks to argument mixed (the


Torus algorithm is mixed with SFMT).
v

0.4

> torus(5, mixed =TRUE)


0.2

[1] 0.64883804 0.07686043 0.32672024


[4] 0.44798899 0.61190152
0.0

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

0.0 0.2 0.4 0.6 0.8 1.0


0 10 20 30 40 50

u Lag

Figure 1: Sobol (two sampling types)


Series torus(10^5, mix = TRUE)

The optional argument useTime can be used to


0.8

the machine time or not to initiate the seed. If


ACF

we do not use the machine time, two calls of


0.4

torus produces obviously the same output.


If we want the random sequence with prime
0.0

number 7, we just type:


0 10 20 30 40 50
> torus(5, p =7)
Lag
[1] 0.6457513 0.2915026 0.9372539
[4] 0.5830052 0.2287566
Figure 2: Auto-correlograms
The dim argument is exactly the same as
congruRand or SFMT. By default, we use the
first prime numbers, e.g. 2, 3 and 5 for a call 1
the first 100 000 prime numbers are take from
like torus(10, 3). But the user can specify http://primes.utm.edu/lists/small/millions/.
4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 15

4.3 Visual comparisons > par(mfrow = c(2,1))


> plot(WELL(1000, 2))
To understand the difference between pseudo
> plot(sobol(10^3, 2, scram=2))
and quasi RNGs, we can make visual com-
parisons of how random numbers fill the unit
square.
First we compare SFMT algorithm with
Torus algorithm on figure 3.
WELL 512a
> par(mfrow = c(2,1))

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.0 0.2 0.4 0.6 0.8 1.0


v

0.4

u
0.2

Sobol (Faure−Tezuka)
0.0

1.0

0.0 0.2 0.4 0.6 0.8 1.0


0.8

u
0.6

Torus
v

0.4
1.0

0.2
0.8

0.0
0.6

0.0 0.2 0.4 0.6 0.8 1.0


v

0.4

u
0.2

Figure 4: WELL vs. Sobol


0.0

0.0 0.2 0.4 0.6 0.8 1.0

4.4 Applications of QMC methods


Figure 3: SFMT vs. Torus algorithm
4.4.1 d dimensional integration

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

compute 4.4.2 Pricing of a Vanilla Call


Z In this sub-section, we will present one fi-
2
Icos (d) = cos(||x||)e||x|| dx nancial application of QMC methods. We
d
R
v  want to price a vanilla European call in the
d/2 n u d framework of a geometric Brownian motion
π X X
(Φ−1 )2 (tij )
u
≈ cos t for the underlying asset. Those options are
n
i=1 j=1 already implemented in the package fOptions
of Rmetrics bundle1 .
where Φ−1 denotes the quantile function of the The payoff of this classical option is
standard normal distribution.
We simply use the following code to compute f (ST ) = (ST − K)+ ,
the Icos (25) integral whose exact value is
−1356914. where K is the strike price. A closed formula
for this call was derived by Black & Scholes
(1973).
> I25 <- -1356914
The Monte Carlo method to price this
> nb <- c(1200, 14500, 214000)
option is quite simple
> ans <- NULL
> for(i in 1:3) 1. simulate sT,i for i = 1 . . . n from starting
+ { point s0 ,
+ tij <- sobol(nb[i], dim=25, 2. compute Pthe mean of the discounted
+ scrambling=2, normal=TRUE) payoff n1 ni=1 e−rT (sT,i − K)+ .
+ Icos <- sqrt(rowSums(tij^2/2))
+ Icos <- mean(cos(Icos)) * pi^(25/2) With parameters (S0 = 100, T = 1, r = 5%,
+ ans <- rbind(ans, c(n=nb[i], K = 60, σ = 20%), we plot the relative error as
+ I25=Icos, Delta=(Icos-I25)/I25)) a function of number of simulation n on figure
+ } 5.
> data.frame(ans) We test two pseudo-random generators
(namely Park Miller and SF-Mersenne
n I25 Delta Twister) and one quasi-random generator
1 1200 -1355379 -1.131527e-03 (Torus algorithm). No code will be shown, see
2 14500 -1357216 2.223429e-04 the file qmc.R in the package source. But we
3 214000 -1356909 -3.870909e-06 use a step-by-step simulation for the Brownian
motion simulation and the inversion function
method for Gaussian distribution simulation
The results obtained from the Sobol Monte
(default in R).
Carlo method in comparison to those obtained
by Papageorgiou & Traub (2000) with a
Vanilla Call
generalized Faure sequence and in comparison
0.02

with the quadrature rules of Namee & Stenger SFMT


Torus
Park Miller
(1967), Genz (1982) and Patterson (1968) are zero
0.01

listed in the following table.


relative error

0.00

n 1200 14500 214000


Faure (P&T) 0.001 0.0005 0.00005
-0.01

Sobol (s=0) 0.02 0.003 0.00006


s=1 0.004 0.0002 0.00005
-0.02

s=2 0.001 0.0002 0.000002


s=3 0.002 0.0009 0.00003 0e+00 2e+04 4e+04 6e+04 8e+04 1e+05

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

As showed on figure 5, the convergence of Down Out Call

Monte Carlo price for the Torus algorithm

0.02
SFMT
Torus
is extremely fast. Whereas for SF-Mersenne Park Miller
zero

Twister and Park Miller prices, the conver-

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

path-dependent option family, i.e. we need to simulation number

simulate whole trajectories of the underlying


asset S on [0, T ]. Figure 6: Error function for Down Out Call
In the same framework of a geometric
Brownian motion, there exists a closed formula √
for DOCs (see Rubinstein & Reiner (1991)). are computed with the sequence (i{ pj })i .
Those options are already implemented in Thanks to the linear independence of the Torus
the package fExoticOptions of Rmetrics sequence over the rationals3 , we guarantee a
bundle2 . non-correlation of Torus quasi-random num-
The payoff of a DOC option is bers.
However, these results do not prove the
f (ST ) = (ST − K)+ 11(τH >T ) , Torus algorithm is always better than tradi-
tional Monte Carlo. The results are sensitive
where K is the strike price, T the maturity, τH to the barrier level H, the strike price X (being
the stopping time associated with the barrier in or out the money has a strong impact), the
H and St the underlying asset at time t. asset volatility σ and the time point number d.
As the price is needed on the whole period Actuaries or readers with actuarial back-
[0, T ], we produc as follows: ground can find an example of actuarial
1. start from point st0 , applications of QMC methods in Albrecher
2. for simulation i = 1 . . . n and time index et al. (2003). This article focuses on simula-
j = 1...d tion methods in ruin models with non-linear
dividend barriers.
• simulate stj ,i ,
• update disactivation boolean Di
3. compute Pthe mean of the discounted 5 Random generation tests
payoff n1 ni=1 e−rT (sT,i − K)+ Di ,
Tests of random generators aim to check if the
where n is the simulation number, d the
output u1 , . . . , un , . . . could be considered as
point number for the grid of time and Di the
independent and identically distributed (i.i.d.)
opposite of boolean Di .
uniform variates for a given confidence level.
In the following, we set T = 1, r = 5%,
There are two kinds of tests of the uniform
st0 = 100, H = K = 50, d = 250 and
distribution: first on the interval ]0, 1[, second
σ = 20%. We test crude Monte Carlo methods
on the binary set {0, 1}. In this note, we only
with Park Miller and SF-Mersenne Twister
describe tests for ]0, 1[ outputs (see L’Ecuyer
generators and a quasi-Monte Carlo method
& Simard (2007) for details about these two
with (multidimensional) Torus algorithm on
kind of tests).
the figure 6.
Some RNG tests can be two-level tests, i.e.
One may wonder why the Torus algorithm
we do not work directly on the RNG output
is still the best (on this example). We use
ui ’s but on a function of the output such as the
the d-dimensional Torus sequence. Thus for
spacings (coordinate difference of the sorted
time tj , the simulated underlying assets (stj ,i )i
sample).
1
DOC is disactived when the underlying asset hits
3 √ √
the barrier. i.e. for k 6= j, ∀i, (i{ pj })i and (i{ pk })i are
2
created by Wuertz et al. (2007a). linearly independent over Q.
5 RANDOM GENERATION TESTS 18

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 Tests based on multiple se- 5.2.3 The φ-divergence test


quences
There exist generalizations of these tests where
Under the i.i.d. hypothesis, a vector of output we take a function of counts Nj , which we
values ui , . . . , ui+t−1 is uniformly distributed called φ-divergence test. Let f be a real valued
over the unit hypercube [0, 1]t . Tests based function. The test statistic is given by
on multiple sequences partition the unit hy-
k−1
percube into cells and compare the number of X
f (Nj ).
points in each cell with the expected number.
j=0

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

note C the number of collisions where C is the random number of different


The distribution of collision number C is cards and 2 Snd the second-kind Stirling num-
given by bers. For a demonstration, go to Knuth (2002).
n−c−1
Y k−i 1 n−c
P (C = c) = 2S , 6 Description of RNG test
k kc n
i=0
functions
where 2 Snk denotes the Stirling number of the
second kind1 and c = 0, . . . , n − 1. In this section, we will give usage examples of
But we cannot use this formula for large RNG test functions, in a similar way as section
n since the Stirling number need O(n log(n)) 4 illustrates section 2 - two first sub-sections.
time to be computed. As L’Ecuyer et al. (2002) The last sub-section focuses on detecting a
we use a Gaussian approximation if λ = nk > particular RNG.
1 8
32 and n ≥ 2 , a Poisson approximation if
1 > par(mfrow = c(2,1))
λ < 32 and the exact formula otherwise.
The normal approximation assumes C fol- > hist(SFMT(10^3), 100)
lows a normal distribution with mean m = > hist(torus(10^3), 100)
n
n − k + k k−1k and variance very complex
(see L’Ecuyer & Simard (2007)). Whereas the
Poisson approximation assumes C follows a 6.1 Test on one sequence of n num-
2
Poisson distribution of parameter n2k . bers
1
they are defined by 2 Snk = k × 2 Sn−1
k k−1
+ 2 Sn−1 and Goodness of Fit tests are already imple-
1 n
2 Sn = 2 Sn = 1. For example go to wikipedia. mented in R with the function ks.test for
6 DESCRIPTION OF RNG TEST FUNCTIONS 20

(sample size : 1000)


Histogram of SFMT(10^3)
length observed freq theoretical freq

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

SFMT(10^3) If you want l = 1/3 and u = 2/3 with a SFMT


sequence, you just type

Histogram of torus(10^3) > gap.test(SFMT(1000), 1/3, 2/3)

6.1.2 The order test


10

The Order test is implemented in function


order.test for d-uples when d = 2, 3, 4, 5. A
8

typical call is as following


Frequency

> order.test(runif(4000), d=4)


4

Order test
2

chisq stat = 25, df = 23


, p-value = 0.34
0

0.0 0.2 0.4 0.6 0.8 1.0 (sample size : 1000)


torus(10^3)
observed number 40 43 38 35 39 41
44 43 43 46 50 43 52 51 43 47 43 30
41 37 25 47 30 49
Kolmogorov-Smirnov test and in package adk
for Anderson-Darling test. In the following, we expected number 42
will focus on one-sequence test implemented in
randtoolbox. Let us notice that the sample length must
be a multiple of dimension d, see sub-section
6.1.1 The gap test 5.1.3.

The function gap.test implements the gap


6.1.3 The frequency test
test as described in sub-section 5.1.2. By
default, lower and upper bound are l = 0 and The frequency test described in sub-section
u = 0.5, just as below. 5.1.4 is just a basic equi-distribution test in
[0, 1] of the generator. We use a sequence
> gap.test(runif(1000)) integer to partition the unit interval and test
counts in each sub-interval.
Gap test
> freq.test(runif(1000), 1:4)
chisq stat = 9.6, df = 10
, p-value = 0.48 Frequency test
6 DESCRIPTION OF RNG TEST FUNCTIONS 21

chisq stat = 2.2, df = 3 chisq stat = 66, df = 19


, p-value = 0.53 , p-value = 4.8e-07

(sample size : 1000) exact distribution


(sample number : 1000/sample size : 128
observed number 235 245 253 267 / cell number : 1024)

collision observed expected


expected number 250
number count count

6.2 Tests based on multiple se- 1 3 2.3


quences 2 14 10
3 26 29
Let us study the serial test, the collision test 4 70 62
and the poker test. 5 105 102
6 139 138
6.2.1 The serial test 7 144 156
Defined in sub-section 5.2.1, the serial test 8 152 151
focuses on the equidistribution of random 9 118 126
numbers in the unit hypercube [0, 1]t . We split 10 101 93
each dimension of the unit cube in d equal 11 56 61
pieces. Currently in function serial.test, we 12 35 36
implement t = 2 and d fixed by the user. 13 22 19
14 9 8.9
> serial.test(runif(3000), 3) 15 3 3.9
16 1 1.5
Serial test 17 1 0.56
18 0 0.19
chisq stat = 5.5, df = 8 19 0 0.058
, p-value = 0.7 20 1 0.017

When the cell number is far greater than


(sample size : 3000)
the sample length, we use the Poisson approx-
observed number 167 169 159 168 imation (i.e. λ < 1/32). For example with
161 176 146 170 184 congruRand generator we have
> coll.test(congruRand, 2^8, 2^14, 1)
expected number 167
Collision test
In newer version, we will add an argument t
chisq stat = 1.5, df = 7
for the dimension.
, p-value = 0.98

6.2.2 The collision test Poisson approximation


(sample number : 1000/sample size : 256
The exact distribution of collision number / cell number : 16384)
costs a lot of time when sample size and cell
number are large (see sub-section 5.2.2). With collision observed expected
function coll.test, we do not yet implement number count count
the normal approximation.
The following example tests Mersenne- 0 135 135
Twister algorithm (default in R) and parame- 1 284 271
ters implying the use of the exact distribution 2 264 271
(i.e. n < 28 and λ > 1/32). 3 180 180
4 90 90
> coll.test(runif, 2^7, 2^10, 1) 5 32 36
6 11 12
Collision test 7 4 3.4
6 DESCRIPTION OF RNG TEST FUNCTIONS 22

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

> for (i in 1:1000) random numbers under any known efficient


+ { procedure. Such generators are typically
+ xold <- xnew slower than Mersenne Twister type generators.
+ xnew <- runif(1) The factor of slow down may be, for example,
+ err <- max(err, abs(wh.predict(xold)-xnew))5. If the simulation problem requires intensive
+ } computation besides the generating random
> print(err) numbers, using slower, but better, generator
may imply only negligible slow down of the
[1] 0 computation as a whole.

The printed error is 0 on some machines and


less than 5 · 10−16 on other machines. This 7 Calling the functions from
is clearly different from the error obtained for other packages
truly random numbers, which is close to 1.
The requirement that the output of a ran- In this section, we briefly present what to do if
dom number generator should not be distin- you want to use this package in your package.
guishable from the truly random numbers by This section is mainly taken from package expm
a simple computation, is also directly related available on R-forge.
to the way, how a generator is used. Typically, Package authors can use facilities from
we use the generated numbers as an input randtoolbox in two ways:
to a computation and we expect that the
• call the R level functions (e.g. torus) in
distribution of the output (for different seeds
R code;
or for different starting indices in the sequence)
is the same as if the input are truly random • if random number generators are needed
numbers. A failure of this assumption implies in C, call the routine torus,. . .
that observing the output of the computation
allows to distinguish the output from the Using R level functions in a package simply
generator from truly random numbers. Hence, requires the following two import directives:
we want to use a generator, for which we
Imports: randtoolbox
may expect that the calculations used in the
intended application cannot distinguish its in file DESCRIPTION and
output from truly random numbers.
In this context, it has to be noted that import(randtoolbox)
many of the currently used generators for
simulations can be distinguished from truly in file NAMESPACE.
random numbers using the arithmetic mod Accessing C level routines further requires to
2 applied to individual bits of the output prototype the function name and to retrieve its
numbers. This is true for Mersenne Twister, pointer in the package initialization function
SFMT and also all WELL generators. The R init pkg , where pkg is the name of the
basis for tolerating this is based on two facts. package.
First, the arithmetic mod 2 and extract- For example if you want to use torus C
ing individual bits of real numbers is not function, you need
directly used in typical simulation problems void (*torus)(double *u, int nb, int dim,
and real valued functions, which represent int *prime, int ismixed, int usetime);
these operations are extremely discontinuous
and such functions also do not typically occur void R_init_pkg(DllInfo *dll)
in simulation problems. Another reason is {
that we need to observe quite a long history torus = (void (*) (double, int, int,
of the output to detect the difference from int, int, int)) \
true randomness. For example, for Mersenne R_GetCCallable("randtoolbox", "torus");
Twister, we need 624 consecutive numbers. }
On the other hand, if we use a cryp-
tographically strong pseudorandom number See file randtoolbox.h to find headers of
generator, we may avoid a difference from truly RNGs. Examples of C calls to other functions
REFERENCES 24

can be found in this package with the WELL References


RNG functions.
The definitive reference for these matters re- Albrecher, H., Kainhofer, R. & Tichy,
mains the Writing R Extensions manual, page R. E. (2003), ‘Simulation methods in ruin
20 in sub-section “specifying imports exports” models with non-linear dividend barriers’,
and page 64 in sub-section “registering native Mathematics and Computers in Simulation
routines”. 62, 277–287. 17
Antonov, I. & Saleev, V. (1979), ‘An eco-
nomic method of computing lp sequences’,
USSR Comp. Mathematics and Mathemati-
cal Physics 19 pp. 252–256. 9
Black, F. & Scholes, M. (1973), ‘The pricing of
options and corporate liabilities’, Journal of
Political Economy 81(3). 16
Bratley, P. & Fox, B. (1988), ‘Algorithm
659: Implementing sobol’s quasi-random
sequence generators’, ACM Transactions on
Mathematical Software 14(88-100). 9
Bratley, P., Fox, B. & Niederreiter, H. (1992),
‘Implementation and tests of low discrep-
ancy sequences’, ACM Transactions Mode;
Comput. Simul. 2(195-213). 9
Eddelbuettel, D. (2007), random: True ran-
dom numbers using random.org.
URL: http://www.random.org 2
Genz, A. (1982), ‘A lagrange extrapolation
algorithm for sequences of approximations
to multiple integrals’, SIAM Journal on
scientific computing 3, 160–172. 16
Hong, H. & Hickernell, F. (2001), Implement-
ing scrambled digital sequences. preprint. 9
Jäckel, P. (2002), Monte Carlo methods in
finace, John Wiley & Sons. 6
Joe, S. & Kuo, F. (1999), Remark on algorithm
659: Implementing sobol’s quasi-random
sequence generator. Preprint. 9
Knuth, D. E. (2002), The Art of Computer
Programming: seminumerical algorithms,
Vol. 2, 3rd edition edn, Massachusetts:
Addison-Wesley. 3, 4, 11, 19
L’Ecuyer, P. (1990), ‘Random numbers for
simulation’, Communications of the ACM
33, 85–98. 2, 3
L’Ecuyer, P. & Simard, R. (2007), ‘Testu01:
A c library for empirical testing of random
number generators’, ACM Trans. on Math-
ematical Software 33(4), 22. 17, 18, 19, 22
REFERENCES 25

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

Niederreiter, H. (1978), ‘Quasi-monte carlo


methods and pseudo-random numbers’, Bul-
letin of the American Mathematical Society
84(6). 2, 6, 7, 8, 10

Niederreiter, H. (1992), Random Number Gen-


eration and Quasi-Monte Carlo Methods,
SIAM, Philadelphia. 6

Panneton, F., L’Ecuyer, P. & Matsumoto,


M. (2006), ‘Improved long-period generators
based on linear recurrences modulo 2’, ACM
Trans. on Mathematical Software 32(1), 1–
16. 4, 5

Papageorgiou, A. & Traub, J. (2000), ‘Faster


evaluation of multidimensional integrals’,
arXiv:physics/0011053v1 p. 10. 16

Park, S. K. & Miller, K. W. (1988), ‘Random


number generators: good ones are hard to
find.’, Association for Computing Machinery
31(10), 1192–2001. 2, 3, 4, 11, 12

You might also like