<<

A note on random number generation

Christophe Dutang and Diethelm Wuertz

September 2009

1 1 INTRODUCTION 2

“Nothing in Nature is random. . . number generation. By “random numbers”, we a thing appears random only through mean random variates of the uniform U(0, 1) the incompleteness of our knowledge.” distribution. More complex distributions can Spinoza, Ethics I1. be generated with uniform variates and rejection or inversion methods. Pseudo random number generation aims to seem random whereas quasi random number generation aims to be determin- istic but well equidistributed.

1 Introduction Those familiars with such as linear congruential generation, Mersenne-Twister type algorithms, and low discrepancy sequences should Random simulation has long been a very popular go directly to the next section. and well studied field of mathematics. There exists a wide range of applications in biology, finance, insurance, physics and many others. So 2.1 Pseudo random generation simulations of random numbers are crucial. In this note, we describe the most random number algorithms At the beginning of the nineties, there was no state-of-the-art algorithms to generate pseudo Let us recall the only things, that are truly ran- random numbers. And the article of Park & dom, are the measurement of physical phenomena Miller (1988) entitled Random generators: good such as thermal noises of semiconductor chips or ones are hard to find is a clear proof. radioactive sources2. Despite this fact, most users thought the rand The only way to simulate some function they used was good, because of a short on computers are carried out by deterministic period and a term to term dependence. But algorithms. Excluding true randomness3, there in 1998, Japenese mathematicians Matsumoto are two kinds random generation: pseudo and and Nishimura invents the first whose quasi random number generators. period (219937 −1) exceeds the number of electron spin changes since the creation of the Universe The package randtoolbox provides func- (106000 against 10120). It was a big breakthrough. tions for pseudo and quasi random number generations, as well as statistical tests to quantify As described in L’Ecuyer (1990), a (pseudo) the quality of generated random numbers. random number generator (RNG) is defined by a structure (S, µ, f, U, g) where

2 Overview of random genera- • S a finite set of states, • µ a probability distribution on S, called the tion algorithms initial distribution, • a transition function f : S 7→ S, • a finite set of output symbols U, In this section, we first the pseudo random • an output function g : S 7→ U. number generation and second the quasi random

1quote taken from Niederreiter (1978). Then the generation of random numbers is as 2for more details go to http://www.random.org/ follows: randomness/. 3For true random number generation on R, use the random package of Eddelbuettel (2007). 1. generate the initial state (called the ) s0 2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 3

according to µ and compute u0 = g(s0), Finally, we generally use one of the three types 2. iterate for i = 1,... , si = f(si−1) and ui = of output function: g(si).

x • g : N 7→ [0, 1[, and g(x) = m , Generally, the seed s0 is determined using the x • g : N 7→]0, 1], and g(x) = m−1 , machine, and so the random variates x+1/2 • g : N 7→]0, 1[, and g(x) = . u0, . . . , un,... seems “real” i.i.d. uniform random m variates. The period of a RNG, a charac- teristic, is the smallest integer p ∈ , such that N Linear congruential generators are implemented ∀n ∈ , s = s . N p+n n in the R function congruRand.

2.1.1 Linear congruential generators 2.1.2 Multiple recursive generators There are many families of RNGs : linear congru- ential, multiple recursive,. . . and “computer oper- A generalisation of linear congruential generators ation” algorithms. Linear congruential generators are multiple recursive generators. They are based have a transfer function of the following type on the following recurrences

f(x) = (ax + c) mod m1, xn = (a1xn−1 + ··· + akxn−kc) mod m, where a is the multiplier, c the increment and m the modulus and x, a, c, m ∈ N (i.e. S is the set where k is a fixed integer. Hence the nth term of of (positive) integers). f is such that the sequence depends on the k previous one. A particular case of this type of generators is when xn = (axn−1 + c) mod m. x = (x + x ) mod 230, Typically, c and m are chosen to be relatively n n−37 n−100 prime and a such that ∀x ∈ , ax mod m 6= 0. N which is a Fibonacci-lagged generator2. The The cycle length of linear congruential generators period is around 2129. This generator has will never exceed modulus m, but can maximised been invented by Knuth (2002) and is generally with the three following conditions called “Knuth-TAOCP-2002” or simply “Knuth- TAOCP”3. • increment c is relatively prime to m, • a − 1 is a multiple of every prime dividing m, An integer version of this generator is im- • a − 1 is a multiple of 4 when m is a multiple plemented in the R function runif (see RNG). of 4, We include in the package the latest double version, which corrects undesirable deficiency. As described on Knuth’s webpage4 , the previous see Knuth (2002) for a proof. version of Knuth-TAOCP fails if we generate few sequences with several seeds. When c = 0, we have the special case of Park- The cures to this problem is to discard the first Miller algorithm or Lehmer algorithm (see Park & 2000 numbers. Miller (1988)). Let us note that the n + jth term can be easily derived from the nth term with a 2see L’Ecuyer (1990). puts to aj mod m (still when c = 0). 3TAOCP stands for The Art Of Computer Program- ming, Knuth’s famous book. 1this representation could be easily generalized for 4go to http://www-cs-faculty.stanford.edu/ matrix, see L’Ecuyer (1990). ˜knuth/news02.html#rng. 2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 4

2.1.3 Mersenne-Twister where >> u (resp. << s) denotes a rightshift (leftshift) of u (s) bits. At last, we transform random integers to reals with one of output These two types of generators are in the big fam- functions g proposed above. ily of matrix linear congruential generators (cf. L’Ecuyer (1990)). But until here, no algorithms Details of the order of the successive operations exploit the binary structure of computers (i.e. used in the Mersenne-Twister (MT) algorithm use binary operations). In 1994, Matsumoto and can be found at the page 7 of Matsumoto & Kurita invented the TT800 generator using binary Nishimura (1998). However, the least, we need operations. But Matsumoto & Nishimura (1998) to learn and to retain, is all these (bitwise) greatly improved the use of binary operations and operations can be easily done in many computer proposed a new random number generator called languages (e.g in C) ensuring a very fast algo- Mersenne-Twister. rithm. Matsumoto & Nishimura (1998) work on the The set of parameters used are finite set N2 = {0, 1}, so a variable x is represented by a vectors of ω bits (e.g. 32 bits). They use the following linear recurrence for the n + ith term: • (ω, n, m, r) = (32, 624, 397, 31), upp low xi+n = xi+m ⊕ (xi |xi+1)A, upp where n > m are constant integers, xi • a = 0 × 9908B0DF, b = 0 × 9D2C5680, c = low (respectively xi ) means the upper (lower) ω − r 0 × EFC60000, (r) bits of xi and A a ω × ω matrix of N2. | is the upp low operator of concatenation, so xi |xi+1 appends the upper ω − r bits of xi with the lower r bits of • u = 11, l = 18, s = 7 and t = 15. xi+1. After a right multiplication with the matrix 1 A , ⊕ adds the result with xi+m bit to bit modulo two (i.e. ⊕ denotes the exclusive-or called xor). These parameters ensure a good equidistribution nω−r 19937 Once provided an initial seed x0, . . . , xn−1, and a period of 2 − 1 = 2 − 1. Mersenne Twister produces random integers in 0,..., 2ω−1. All operations used in the recurrence The great advantages of the MT algorithm are are bitwise operations, thus it is a very fast a far longer period than any previous generators computation compared to modulus operations (greater than the period of Park & Miller (1988) used in previous algorithms. sequence of 232 − 1 or the period of Knuth (2002) around 2129), a far better equidistribution (since To increase the equidistribution, Matsumoto & it passed the DieHard test) as well as an very Nishimura (1998) added a tempering step: good computation time (since it used binary operations and not the costly real operation yi ← xi+n ⊕ (xi+n >> u), modullus). yi ← yi ⊕ ((yi << s) ⊕ b),

yi ← yi ⊕ ((yi << t) ⊕ c), MT algorithm is already implemented in R (function runif). However the package yi ← yi ⊕ (yi >> l), randtoolbox provide functions to compute a  0 I  1Matrix A equals to ω−1 whose right multi- new version of Mersenne-Twister (the SIMD- a oriented Fast Mersenne Twister algorithm) as well plication can be done with a bitwise rightshift operation and an addition with integer a. See the section 2 of as the WELL (Well Equidistributed Long-period Matsumoto & Nishimura (1998) for explanations. Linear) generator. 2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 5

2.1.4 Well Equidistributed Long-period An usual measure of uniformity is the sum of Linear generators dimension gaps

ω X The MT recurrence can be rewritten as ∆1 = δl. l=1

xi = Axi−1, Panneton et al. (2006) tries to find generators with a dimension gap sum ∆1 around zero and a where x are vectors of N and A a transition k 2 number Z1 of non-zero coefficients in χA around matrix. The charateristic polynom of A is k/2. Generators with these two characteristics are called Well Equidistributed Long-period Linear 4 k k−1 χA(z) = det(A−zI) = z −α1z −· · ·−αk−1z−αk, generators. As a benchmark, Mersenne Twister algorithm is characterized with k = 19937, ∆1 = with coefficients αk’s in N2. Those coefficients are 6750 and Z1 = 135. linked with output integers by The WELL generator is characterized by the xi,j = (α1xi−1,j + ··· + αkxi−k,j) mod 2 following A matrix   for all component j. T5,7,0 0 ...  T 0 ...   0   ..  From Panneton et al. (2006), we have the  0 I .    , period length of the recurrence reaches the upper  .. ..  k  . .  bound 2 − 1 if and only if the polynom χA is a    ..  primitive polynomial over N2.  . I 0  0 L 0 The more complex is the matrix A the slower will be the associated generator. Thus, where T. are specific matrices, I the identity we compromise between speed and quality (of matrix and L has ones on its “top left” corner. The first two lines are not entirely sparse but “fill” equidistribution). If we denote by ψd the set of all d-dimensional vectors produced by the generator with T. matrices. All T.’s matrices are here to from all initial states1. change the state in a very efficient way, while the subdiagonal (nearly full of ones) is used to shift If we divide each dimension into 2l2 cells (i.e. the unmodified part of the current state to the the unit hypercube [0, 1[d is divided into 2ld cells), next one. See Panneton et al. (2006) for details. the set ψd is said to be (d, l)-equidistributed if and only if each contains exactly 2k−dl of its The MT generator can be obtained with points. The largest dimension for which the set special values of T.’s matrices. Panneton et al. (2006) proposes a set of parameters, where they ψd is (d, l)-equidistributed is denoted by dl. computed dimension gap number ∆1. The full The great advantage of using this definition table can be found in Panneton et al. (2006), we is we are not forced to compute random points only sum up parameters for those implemented in to know the uniformity of a generator. Indeed, this package in table 1. thanks to the linear structure of the recurrence we can express the property of bits of the current Let us note that for the last two generators state. From this we define a dimension gap for l a tempering step is possible in order to have maximally equidistributed generator (i.e. (d, l)- bits resolution as δl = bk/lc − dl. equidistributed for all d and l). These generators 1 k The cardinality of ψd is 2 . are implemented in this package thanks to the C 2with l an integer such that l ≤ bk/dc. code of L’Ecuyer and Panneton. 2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 6

name k N ∆  128  1 1 • wA = w << 8 ⊕ w, WELL512a 512 225 0   WELL1024a 1024 407 0 32 • 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, 2.1.5 SIMD-oriented Fast Mersenne Twister algorithms 128 32 where << denotes a 128-bit operation while >> A decade after the invention of MT, Matsumoto a 32-bit operation, i.e. an operation on the four & Saito (2008) enhances their algorithm with the 32-bit parts of 128-bit word w. computer of today, which have Single Instruction Mutiple Data operations letting to work concep- Hence the transition function of SFMT is given tually with 128 bits integers. by

MT and its successor are part of the family ω n ω n of multiple-recursive matrix generators since they f :(N2 ) 7→ (N2 ) verify a multiple recursive equation with matrix (ω0, . . . , ωn−1) 7→ (ω1, . . . , ωn−1, h(ω0, . . . , ωn−1)), constants. For MT, we have the following recurrence ω n where (N2 ) is the state space.

xk+n =     The selection of recursion and parameters Iω−r 0 0 0 xk A ⊕ xk+1 A ⊕ xk+m. was carried out to find a good dimension of 0 0 0 Ir equidistribution for a given a period. This step is | {z } h(xk,xk+1,...,xm,...,xk+n−1) done by studying the characteristic polynomial of f. SFMT allow periods of 2p − 1 with p a (prime) for the k + nth term. Mersenne exponent1. Matsumoto & Saito (2008) proposes the following set of exponents 607, 1279, Thus the MT recursion is entirely characterized 2281, 4253, 11213, 19937, 44497, 86243, 132049 by and 216091. h(ω0, . . . , ωn−1) = (ω0|ω1)A ⊕ ωm, The advantage of SFMT over MT is the where ωi denotes the ith word integer (i.e. computation speed, SFMT is twice faster without horizontal vectors of N2). SIMD operations and nearly fourt times faster with SIMD operations. SFMT has also a better The general recurrence for the SFMT algorithm equidistribution2 and a better recovery time from extends MT recursion to zeros-excess states3. The function SFMT provides an interface to the C code of Matsumoto and h(ω , . . . , ω ) = ω A ⊕ ω B ⊕ ω C ⊕ ω D, 0 n−1 0 m n−2 n−1 Saito. where A, B, C, D are sparse matrices over N2, ωi are 128-bit integers and the degree of recursion is 1a Mersenne exponent is a prime number p such that p p n =  19937  = 156. 2 − 1 is prime. Prime numbers of the form 2 − 1 have 128 the special designation Mersenne numbers. 2See linear algebra arguments of Matsumoto & The matrices A, B, C and D for a word w are Nishimura (1998). defined as follows, 3states with too many zeros. 2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 7

2.2 Quasi random generation indicator function of subset J. The problem is that our discrete sequence will never constitute a “fair” distribution in Id, since there will always Before explaining and detailing quasi random be a small subset with no points. generation, we must (quickly) explain Monte- Carlo1 methods, which have been introduced in Therefore, we need to consider a more flexible the forties. In this section, we follow the approach definition of uniform distribution of a sequence. of Niederreiter (1978). Before introducing the discrepancy, we need to define Card (u , . . . , u ) as Pn 11 (u ) the Let us work on the d-dimensional unit cube E 1 n i=1 E i number of points in subset E. Then the Id = [0, 1]d and with a (multivariate) bounded discrepancy D of the n points (u ) in Id (Lebesgues) integrable function f on Id. Then we n i 1≤i≤n is given by define the Monte Carlo approximation of integral of f over Id by CardJ (u1, . . . , un) n Dn = sup − λd(J) Z 1 X J∈J n f(x)dx ≈ f(Xi), Id n i=1 where J denotes the family of all subintervals of d Qd where (Xi)1≤i≤n are independent random points I of the form i=1[ai, bi]. If we took the family d d Qd from I . of all subintervals of I of the form i=1[0, bi], Dn is called the star discrepancy (cf. Niederreiter The strong law of large numbers ensures the (1992)). almost surely convergence of the approximation. Furthermore, the expected integration error is Let us note that the Dn discrepancy is nothing √1 bounded by O( n ), with the interesting fact it else than the L∞-norm over the unit cube of the does not depend on dimension d. Thus Monte difference between the empirical ratio of points Carlo methods have a wide range of applications. (ui)1≤i≤n in a subset J and the theoretical point number in J.A L2-norm can be defined as well, The main difference between (pseudo) Monte- see Niederreiter (1992) or J¨ackel (2002). Carlo methods and quasi Monte-Carlo methods is that we no longer use random points (xi)1≤i≤n The integral error is bounded by but deterministic points. Unlike statistical tests, numerical integration does not rely on true 1 n Z X randomness. Let us note that quasi Monte- f(ui) − f(x)dx ≤ Vd(f)Dn, n Id Carlo methods date from the fifties, and have also i=1 been used for interpolation problems and integral where Vd(f) is the d-dimensional Hardy and equations solving. Krause variation2 of f on Id (supposed to be finite). In the following, we consider a sequence ”’Furthermore the convergence condition on the Actually the integral error bound is the product sequence (ui)i is to be uniformly distributed in of two independent quantities: the variability of the unit cube Id with the following sense: function f through Vd(f) and the regularity of the n sequence through D . So, we want to minimize d 1 X n ∀J ⊂ I , lim 11J (ui) = λd(J), n→+∞ n the discrepancy Dn since we generally do not have i=1 a choice in the “problem” function f. where λd stands for the d-dimensional volume (i.e. 2Interested readers can find the definition page 966 of the d-dimensional Lebesgue measure) and 11J the Niederreiter (1978). In a sentence, the Hardy and Krause 1according to wikipedia the name comes from a famous variation of f is the supremum of sums of d-dimensional casino in Monaco. delta operators applied to function f. 2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 8

We will not explain it but this concept can be for Dn: d r ! extented to subset J of the unit cube I in order log log n R Dn = O . to have a similar bound for J f(x)dx. n

In the literature, there were many ways to find sequences with small discrepancy, generally 2.2.2 Van der Corput sequences called low-discrepancy sequences or quasi-random points. A first approach tries to find bounds for these sequences and to search the good An example of quasi-random points, which have a parameters to reach the lower bound or to low discrepancy, is the (unidimensional) Van der decrease the upper bound. Another school tries Corput sequences. to exploit regularity of function f to decrease the discrepancy. Sequences coming from the first Let p be a prime number. Every integer n can school are called quasi-random points while those be decomposed in the p basis, i.e. there exists of the second school are called good lattice points. some integer k such that

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

2.2.3 Halton sequences i where Cj denotes standard combination j! i!(j−i)! . Then we take the radical-inversion The d-dimensional version of the Van der Corput φp(aD,1, . . . , aD,k) defined as sequence is known as the Halton sequence. The k nth term of the sequence is define as X aj φp(a1, . . . , ak) = , d pj+1 (φp1 (n), . . . , φpd (n)) ∈ I , j=1 where p1, . . . , pd are pairwise relatively prime which is the same as above for n defined by the bases. The discrepancy of the Halton sequence aD,i’s.  log(n)d  is asymptotically O n . Finally the (d-dimensional) Faure sequence is The following Halton theorem gives us better defined by discrepancy estimate of finite sequences. For any d dimension d ≥ 1, there exists an finite sequence (φp(a1,1, . . . , a1,k), . . . , φp(ad,1, . . . , ad,k)) ∈ I . d of points in I such that the discrepancy In the bidimensional case, we work in 3-basis, first log(n)d−1  terms of the sequence are listed in table 3. D = O 1. n n n a a a 2 a a a φ(a ..) φ(a ..) Therefore, we have a significant guarantee there 13 12 11 23 22 21 13 23 0 000 000 0 0 exists quasi-random points which are outperform- 1 001 001 1/3 1/3 ing than traditional Monte-Carlo methods. 2 002 002 2/3 2/3 3 010 012 1/9 7/9 4 011 010 4/9 1/9 2.2.4 Faure sequences 5 012 011 7/9 4/9 6 020 021 2/9 5/9 The Faure sequences is also based on the decom- 7 021 022 5/9 8/9 position of integers into prime-basis but they have 8 022 020 8/9 2/9 two differences: it uses only one prime number for 9 100 100 1/27 1/27 basis and it permutes vector elements from one 10 101 101 10/27 10/27 dimension to another. 11 102 102 19/27 19/27 12 110 112 4/27 22/27 The basis prime number is chosen as the small- 13 111 110 12/27 4/27 est prime number greater than the dimension d, 14 112 111 22/27 12/27 i.e. 3 when d = 2, 5 when d = 3 or 4 etc. . . In the Van der Corput sequence, we decompose Table 3: Faure first terms integer n into the p-basis: k X n = a pj. j 2.2.5 Sobol sequences j=1

Let a1,j be integer aj used for the decomposition of n. Now we define a recursive permutation of This sub-section is taken from unpublished work aj: of Diethelm Wuertz. k X i The Sobol sequence xn = (xn,1, . . . , xn,d) is ∀2 ≤ D ≤ d, aD,j = C aD−1,j mod p, j generated from a set of binary functions of j=i length ω bits (vi,j with i = 1, . . . , ω and j = 1if the sequence has at least two points, cf. Niederreiter (1978). 2we omit commas for simplicity. 2 OVERVIEW OF RANDOM GENERATION ALGORITHMS 10

1, . . . , d). vi,j, generally called direction numbers • Equidistribution: The Xi form a point set are numbers related to primitive (irreducible) with probability 1; i.e. the random- ization polynomials over the field {0, 1}. process has preserved whatever special prop- erties the underlying point set had. In order to generate the jth dimension, we sup- pose that the primitive polynomial in dimension The Sobol sequences can be scrambled by the j is Owen’s type of scrambling, by the Faure-Tezuka q−1 type of scrambling, and by a combination of both. pj(x) = x + a1x + ··· + aq−1x + 1.

Then we define the following q-term recurrence The program we have interfaced to R is based relation on integers (Mi,j)i on the ACM Algorithm 659 described by Bratley & Fox (1988) and Bratley et al. (1992). Modi- M = 2a M ⊕ 22a M ⊕ ... i,j 1 i−1,j 2 i−2,j fications by Hong & Hickernell (2001) allow for q−1 q ⊕ 2 aq−1Mi−q+1,j ⊕ 2 aqMi−q,j ⊕ Mi−q a of the sequences. Furthermore, where i > q. in the case of the Sobol sequence we followed the implementation of Joe & Kuo (1999) which can This allow to compute direction numbers as handle up to 1111 dimensions. i vi,j = Mi,j/2 . To interface the Fortran routines to the R envi- This recurrence is initialized by the set of ronment some modifications had to be performed. ω q One important point was to make possible to arbitrary odd integers v1,j2 , . . . , v,j2 ω, which are smaller than 2,..., 2q respectively. Finally re-initialize a sequence and to recall a sequence the jth dimension of the nth term of the Sobol without renitialization from R. This required to sequence is with remove BLOCKDATA, COMMON and SAVE statements from the original code and to pass the xn,j = b1v1,j ⊕ b2v2,j ⊕ · · · ⊕ vω,j, initialization variables through the argument lists Pω−1 k of the subroutines, so that these variables can be where bk’s are the bits of integer n = k=0 bk2 . The requirement is to use a different primitive accessed from R. polynomial in each dimension. An e?cient variant to implement the generation of Sobol sequences was proposed by Antonov & Saleev (1979). The 2.2.7 Kronecker sequences use of this approach is demonstrated in Bratley & Fox (1988) and Press et al. (1996). Another kind of low-discrepancy sequence uses irrational number and fractional part. The fractional part of a real x is denoted by {x} = 2.2.6 Scrambled Sobol sequences x − bxc. The infinite sequence ({nα})n≤0 has a bound for its discrepancy Randomized QMC methods are the basis for error 1 + log n D ≤ C . estimation. A generic recipe is the following: n n Let A1,...,An be a QMC point set and Xi a This family of infinite sequence ({nα})n≤0 is scrambled version of Ai. Then we are searching called the Kronecker sequence. for randomizations which have the following properties: A special case of the Kronecker sequence is the Torus algorithm where irrational number α is a root of a prime number. The nth term of • Uniformity: The Xi makes the approximator ˆ 1 PN the d-dimensional Torus algorithm is defined by I = N i=1 f(Xi) an unbiased estimate of R √ √ d I = [0,1]d f(x)dx. ({n p1},..., {n pd}) ∈ I , 3 EXAMPLES OF DISTINGUISHING FROM TRULY RANDOM NUMBERS 11

where (p1, . . . , pd) are prime numbers, generally We have the following theorem for good lattice the first d prime numbers. With the previous points. For every dimension d ≥ 2 and integer d inequality, we can derive an estimate of the Torus n ≥ 2, there exists a lattice points g ∈ Z which algorithm discrepancy: coordinates relatively prime to n such that the 1 n discrepancy Dn of points { g},..., { g} satisfies 1 + log n n n O . n d 1 7 d D < + + 2 log m . s n 2n 5

2.2.8 Mixed pseudo quasi random se- quences Numerous studies of good lattice points try to find point g which minimizes the discrep- ancy. Korobov test g of the following form Sometimes we want to use quasi-random se- 1, m, . . . , md−1 with m ∈ . Bahvalov tries quences as pseudo random ones, i.e. we want to N Fibonnaci numbers (F1,...,Fd). Other studies keep the good equidistribution of quasi-random g look directly for the point α = n e.g. α = points but without the term-to-term dependence.  1 d  p d+1 , . . . , p d+1 or some cosinus functions. We One way to solve this problem is to use pseudo let interested readers to look for detailed informa- random generation to mix outputs of a quasi- tion in Niederreiter (1978). random sequence. For example in the case of the Torus sequence, we have repeat for 1 ≤ i ≤ n 3 Examples of distinguishing • draw an integer ni from Mersenne-Twister in from truly random numbers {0,..., 2ω − 1} √ • then ui = {ni p} For a good generator, it is not computationally easy to distinguish the output of the generator 2.2.9 Good lattice points from truly random numbers, if the seed or the index in the sequence is not known. In this section, we present examples of generators, whose In the above methods we do not take into account output may be easily distinguished from truly a better regularity of the integrand function f random numbers. than to be of bounded variation in the sense of Hardy and Krause. Good lattice point sequences An example of such a generator is the older try to use the eventual better regularity of f. version of Wichmann-Hill from 1982. For this generator, we can even predict the next number If f is 1-periodic for each variable, then the in the sequence, if we know the last already approximation with good lattice points is generated one. Verifying such a predicition is easy n and it is, of course, not valid for truly random Z 1 X  i  f(x)dx ≈ f g , numbers. Hence, we can easily distinguish d n n I i=1 the output of the generator from truly random numbers. An implementation of this test in R d where g ∈ Z is suitable d-dimensional lattice derived from McCullough (2008) is as follows. point. To impose f to be 1-periodic may seem too brutal. But there exists a method to transform f into a 1-periodic function while > wh.predict <- function(x) preserving regularity and value of the integrand + { (see Niederreiter 1978, page 983). + M1 <- 30269 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 12

+ M2 <- 30307 In this context, it has to be noted that many + M3 <- 30323 of the currently used generators for simulations + y <- round(M1*M2*M3*x) can be distinguished from truly random numbers + s1 <- y %% M1 using the arithmetic mod 2 (XOR operation) + s2 <- y %% M2 applied to individual bits of the output numbers. + s3 <- y %% M3 This is true for Mersenne Twister, SFMT and also + s1 <- (171*26478*s1) %% M1 all WELL generators. The basis for tolerating this + s2 <- (172*26070*s2) %% M2 is based on two facts. First, the arithmetic mod 2 + s3 <- (170*8037*s3) %% M3 and extracting individual bits of real numbers is + (s1/M1 + s2/M2 + s3/M3) %% 1 not directly used in typical simulation problems + } and real valued functions, which represent these > RNGkind("Wichmann-Hill") operations, are extremely discontinuous and such > xnew <- runif(1) functions also do not typically occur in simulation > maxerr <- 0 problems. Another reason is that we need to > for (i in 1:1000) { observe quite a long history of the output to + xold <- xnew detect the difference from true randomness. For + xnew <- runif(1) example, for Mersenne Twister, we need 624 + err <- abs(wh.predict(xold) - xnew)consecutive numbers. + maxerr <- max(err, maxerr) + } On the other hand, if we use a cryptograph- > print(maxerr) ically strong pseudorandom number generator, we may avoid distinguishing from truly ran- dom numbers under any known efficient proce- [1] 0 dure. Such generators are typically slower than Mersenne Twister type generators. The factor of slow down is, for example for AES, about The printed error is 0 on some machines and 5. However, note that for simulation problems, less than 5 · 10−16 on other machines. This is which require intensive computation besides the clearly different from the error obtained for truly generating random numbers, using slower, but random numbers, which is close to 1. better, generator implies only negligible slow down of the computation as a whole. The requirement that the output of a random number generator should not be distinguishable from the truly random numbers by a simple computation, is directly related to the way, how a 4 Description of the random generator is used. Typically, we use the generated generation functions numbers as an input to a computation and we expect that the distribution of the output (for different seeds or for different starting indices In this section, we detail the R functions imple- in the sequence) is the same as if the input mented in randtoolbox and give examples. are truly random numbers. A failure of this assumption implies, besides of a wrong result of our simulation, that observing the output of 4.1 Pseudo random generation the computation allows to distinguish the output from the generator from truly random numbers. Hence, we want to use a generator, for which For pseudo random generation, R provides we may expect that the calculations used in the many algorithms through the function runif intended application cannot distinguish its output parametrized with .Random.seed. We encour- from truly random numbers. age readers to look in the corresponding help 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 13 pages for examples and usage of those functions. > setSeed(1) Let us just say runif use the Mersenne-Twister > congruRand(10, echo=TRUE) algorithm by default and other generators such as Wichmann-Hill, Marsaglia-Multicarry or Knuth- TAOCP-20021. 1 th integer generated : 1 2 th integer generated : 16807 3 th integer generated : 282475249 4.1.1 congruRand 4 th integer generated : 1622650073 5 th integer generated : 984943658 6 th integer generated : 1144108930 The randtoolbox package provides two pseudo- 7 th integer generated : 470211272 random generators functions : congruRand and 8 th integer generated : 101027544 SFMT. congruRand computes linear congruen- 9 th integer generated : 1457850878 tial generators, see sub-section 2.1.1. By default, 10 th integer generated : 1458777923 it computes the Park & Miller (1988) sequence, so [1] 7.826369e-06 1.315378e-01 it needs only the observation number argument. [3] 7.556053e-01 4.586501e-01 If we want to generate 10 random numbers, we [5] 5.327672e-01 2.189592e-01 type [7] 4.704462e-02 6.788647e-01 [9] 6.792964e-01 9.346929e-01 > congruRand(10) We can check that those integers are the 10 first [1] 0.52407351 0.10353304 0.07985553 terms are listed in table 4, coming from http: [4] 0.13184734 0.95818429 0.20331830 //www.firstpr.com.au/dsp/rand31/. [7] 0.17069639 0.89415530 0.06811436 [10] 0.79797631 n xn n xn 1 16807 6 470211272 2 282475249 7 101027544 One will quickly note that two calls to 3 1622650073 8 1457850878 congruRand will not produce the same output. 4 984943658 9 1458777923 This is due to the fact that we use the machine 5 1144108930 10 2007237709 time to initiate the sequence at each call. But the user can set the seed with the function setSeed: Table 4: 10 first integers of Park & Miller (1988) sequence

> setSeed(1) > congruRand(10) We can also check around the 10000th term. From the site http://www.firstpr.com. au/dsp/rand31/, we know that 9998th to [1] 7.826369e-06 1.315378e-01 10002th terms of the Park-Miller sequence are [3] 7.556053e-01 4.586501e-01 925166085, 1484786315, 1043618065, 1589873406, [5] 5.327672e-01 2.189592e-01 2010798668. The congruRand generates [7] 4.704462e-02 6.788647e-01 [9] 6.792964e-01 9.346929e-01 > setSeed(1614852353) > congruRand(5, echo=TRUE) One can follow the evolution of the nth integer generated with the option echo=TRUE.

1see Wichmann & Hill (1982), Marsaglia (1994) and 1 th integer generated : 1614852353 Knuth (2002) for details. 2 th integer generated : 925166085 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 14

3 th integer generated : 1484786315 make comparisons with other algorithms. The 4 th integer generated : 1043618065 Park-Miller RNG should not be viewed as a 5 th integer generated : 1589873406 “good” random generator. [1] 0.4308140 0.6914075 0.4859725 [4] 0.7403425 0.9363511 Finally, congruRand function has a dim argument to generate dim- dimensional vectors of random numbers. The nth vector is build with with 1614852353 being the 9997th term of Park- d consecutive numbers of the RNG sequence (i.e. Miller sequence. the nth term is the (un+1, . . . , un+d)).

However, we are not limited to the Park- Miller sequence. If we change the modulus, 4.1.2 SFMT the increment and the multiplier, we get other random sequences. For example, The SF- Mersenne Twister algorithm is described in sub-section 2.1.5. Usage of SFMT function im- > setSeed(12) plementing the SF-Mersenne Twister algorithm > congruRand(5, mod = 2ˆ8, mult = 25, incris = the 16, same. echo= First TRUE) argument n is the number of random variates, second argument dim the dimension. 1 th integer generated : 12 2 th integer generated : 60 3 th integer generated : 236 > SFMT(10) 4 th integer generated : 28 > SFMT(5, 2) #bi dimensional variates 5 th integer generated : 204 [1] 0.234375 0.921875 0.109375 [4] 0.796875 0.984375 [1] 0.75239882 0.06288682 0.86930538 [4] 0.51724864 0.05710520 0.03491450 [7] 0.29037781 0.27347585 0.89662210 Those values are correct according to Planchet [10] 0.19357430 et al. 2005, page 119. [,1] [,2] Here is a example list of RNGs computable with [1,] 0.31585319 0.75158169 congruRand: [2,] 0.69685564 0.30928903 RNG mod mult incr [3,] 0.90287964 0.36483240 Knuth - Lewis 232 1664525 1.01e91 [4,] 0.05256188 0.02010946 Lavaux - Jenssens 248 31167285 1 [5,] 0.70565521 0.78862669 Haynes 264 6.36e172 1 Marsaglia 232 69069 0 A third argument is mexp for Mersenne expo- Park - Miller 231 − 1 16807 0 nent with possible values (607, 1279, 2281, 4253, Table 5: some linear RNGs 11213, 19937, 44497, 86243, 132049 and 216091). Below an example with a period of 2607 − 1:

One may wonder why we implement such > SFMT(10, mexp = 607) a short-period algorithm since we know the Mersenne-Twister algorithm. It is provided to

11013904223. [1] 0.6224103 0.3086716 0.8260362 2636412233846793005. [4] 0.6994521 0.4362783 0.2404594 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 15

[7] 0.6036481 0.4909854 0.3193069 [6,] 0.3750 0.22222222 [10] 0.4222515 [7,] 0.8750 0.55555556 [8,] 0.0625 0.88888889 [9,] 0.5625 0.03703704 Furthermore, following the advice of Mat- [10,] 0.3125 0.37037037 sumoto & Saito (2008) for each exponent below 19937, SFMT uses a different set of parameters1 in order to increase the independence of random You can use the init argument set to FALSE generated variates between two calls. Otherwise (default is TRUE) if you want that two calls (for greater exponent than 19937) we use one set to halton functions do not produce the same of parameters2. sequence (but the second call continues the sequence from the first call. We must precise that we do not implement the SFMT algorithm, we “just” use the C code of Matsumoto & Saito (2008). For the moment, > halton(5) we do not fully use the strength of their code. > halton(5, init=FALSE) For example, we do not use block generation and SSE2 SIMD operations. [1] 0.500 0.250 0.750 0.125 0.625

4.2 Quasi-random generation [1] 0.3750 0.8750 0.0625 0.5625 0.3125

4.2.1 Halton sequences init argument is also available for other quasi- The function halton implements both the Van RNG functions. Der Corput (unidimensional) and Halton se- quences. The usage is similar to pseudo-RNG functions 4.2.2 Sobol sequences

> halton(10) The function sobol implements the Sobol se- > halton(10, 2) quences with optional (Owen, Faure- Tezuka or both type of sampling). This sub- section also comes from an unpublished work of [1] 0.5000 0.2500 0.7500 0.1250 0.6250 Diethelm Wuertz. [6] 0.3750 0.8750 0.0625 0.5625 0.3125 To use the different scrambling option, you just to use the scrambling argument: 0 for (the [,1] [,2] default) no scrambling, 1 for Owen, 2 for Faure- [1,] 0.5000 0.33333333 Tezuka and 3 for both types of scrambling. [2,] 0.2500 0.66666667 [3,] 0.7500 0.11111111 [4,] 0.1250 0.44444444 > sobol(10) [5,] 0.6250 0.77777778 > sobol(10, scramb=3)

1this can be avoided with usepset argument to FALSE. 2These parameter sets can be found in the C function [1] 0.5000 0.7500 0.2500 0.3750 0.8750 initSFMT in SFMT.c source file. [6] 0.6250 0.1250 0.1875 0.6875 0.9375 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 16

[1] 0.08301502 0.40333283 0.79155722 [4] 0.90135310 0.29438378 0.22406132 Sobol (no scrambling) [7] 0.58105056 0.62985137 0.05026817

[10] 0.49558967 ● ●● ●● ●● ● ●● ●●● ●● ●● ● ● ●● ● ● ● ●● ● 1.0 ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●●● ● ● ●● ● ● ●●● ● ● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ●● ●● ●●●● ● ●● ●●● ●● ● ● ● ● ● ● ● ●●● ●● ●●● ●● ● ● ● ●● ● ● ● ● ● ● ●●● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● 0.8 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ●●● ●● ● ● It is easier to see the impact of scrambling ● ● ● ● ● ● ●● ● ●● ●●● ●● ●●● ● ● ● ● ● ●● ● ● ● ●●● ● ● ●●● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● by plotting two-dimensional sequence in the unit ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ●● ● ● ●●● ● ● ●● ● ● ● ●●● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ●● ● ● ●● ●● ● ● ● ● ●● ● 0.6 ●● ● ● ●● ● ●● ●● ●● ●● ● ●● square. Below we plot the default Sobol sequence ● ●● ●● ● ●● ●● ● ●● ●● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ● ● v ● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ●● ● ● ● and Sobol scrambled by Owen algorithm, see ●● ● ● ● ● ● ● ●● ● ●● ●● ● ●● ● ●● ●● ● ●● ●● ●● ● ●● ● ●● ● ● ●● ● ●● ●● ● ●● ● ● ●● ●● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ●● ● ● ● ●● ● figure 1. 0.4 ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ●● ● ●● ●● ● ●● ● ● ● ● ●● ● ● ●● ●● ● ●● ● ●● ● ●● ●● ●● ● ●● ●● ● ● ●● ●● ● ●● ●● ● ●● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ●● ● ● ●● ● ● ● ●● ●● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● 0.2 ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●●● ● ●● ● ●●● ● ●● ● > par(mfrow = c(2,1)) ● ● ● ●● ● ● ● ●● ● ● ●●● ● ● ● ● ● ● ●● ● ● ● ●● ●● ● ● ●● ● ●● ● ●● ● ● ●● ● ●● ●● ● ● ● ●● ● ● ● ●● ● ●● ● ●● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● > plot(sobol(1000, 2)) ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● > plot(sobol(10ˆ3, 2, scram=1)) 0.0 0.0 0.2 0.4 0.6 0.8 1.0

u

4.2.3 Faure sequences Sobol (Owen)

In a near future, randtoolbox package will have ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ●●● ● ● ● ● ● 1.0 ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ● ● ●● ●● ● ● ● ●● ● ● an implementation of Faure sequences. For the ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●●● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ●● ●● ● ●● ● ● ● ●● ● ● ● ●● ● ●●● ● ●● ●● ● ● ●● moment, there is no function faure. ● ● ● ●● ●● ● ● ● ●● ● ● ● ● ●● ● ● ● ●● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ●● ● 0.8 ● ● ●● ● ● ● ● ● ●● ● ● ● ●● ● ●●● ● ●● ● ●●● ● ● ● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ●● ●● ● ●● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ● ●●● ●● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ●● ● ●● ● ●● ●● ● ● ●● ●● ● ● ● ● ● ●●● ● ●●● ● ● ● ● ● 0.6 ● ● ●● ●● ●● ●● ●● ● ●● ●● ● ●● ●● ● ● ● ● ● ●●● ● ●● ● ● ● 4.2.4 Torus algorithm (or Kronecker se- ● ●● ● ●● ● ●● ●● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ● v ●● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ●●● ● ● ● ● ● ● ● quence) ● ●● ● ● ● ●● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ●● ●● ● ●● ● ● ● ●● 0.4 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ●● ●● ● ●● ● ● ● ● ●● ● ●● ● ● ●● ● ● ● ●●● ● ●● ●● ●● ●● ● ●● ● ●● ●● ● ●● ●● ●● ●● ● ●● ●● ● ●● ● ● ● ●● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● ●●● ● ● ● ● ●● ● ● ● ● ●● ●●● ● ●● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●● ● ● ● The function torus implements the Torus algo- ● ●● ● ● ●●● ● ● ●●● ● ●● ● ● ● ● ●● ●● ● ●● ● ●● ●● ● ● ● ● ●● 0.2 ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● rithm. ● ●● ● ● ● ●● ● ●● ●● ● ●● ● ●● ●●● ● ● ●● ●● ● ● ● ●● ●● ●● ● ●● ●● ● ● ●● ● ● ● ●● ●● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.0

> torus(10) 0.0 0.2 0.4 0.6 0.8 1.0

u [1] 0.41421356 0.82842712 0.24264069 [4] 0.65685425 0.07106781 0.48528137 Figure 1: Sobol (two sampling types) [7] 0.89949494 0.31370850 0.72792206 [10] 0.14213562 [1] 0.43325219 0.84746575 0.26167932 [4] 0.67589288 0.09010644 √These√ numbers√ are fractional parts of 2, 2 2, 3 2,... , see sub-section 2.2.1 for details. The optional argument useTime can be used to the machine time or not to initiate the seed. If we do not use the machine time, two calls of torus > torus(5, use =TRUE) produces obviously the same output. 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 17

If we want the random sequence with prime number 7, we just type: Series torus(10^5)

> torus(5, p =7)

[1] 0.6457513 0.2915026 0.9372539 0.5

[4] 0.5830052 0.2287566 ACF

The dim argument is exactly the same as −0.5 congruRand or SFMT. By default, we use the 0 10 20 30 40 50 first prime numbers, e.g. 2, 3 and 5 for a call like torus(10, 3). But the user can specify a set Lag of prime numbers, e.g. torus(10, 3, c(7, 11, 13)). The dimension argument is limited to 100 0001. Series torus(10^5, mix = TRUE)

As described in sub-section 2.2.8, one way to with serial dependence is to mix the Torus algorithm with a pseudo random generator. The 0.8

torus function offers this operation thanks to ACF 0.4 argument mixed (the Torus algorithm is mixed with SFMT). 0.0 0 10 20 30 40 50 > torus(5, mixed =TRUE) Lag [1] 0.3893373 0.8040533 0.5291147 [4] 0.7903221 0.7474093 Figure 2: Auto-correlograms

In order to see the difference between, we can plot First we compare SFMT algorithm with Torus the empirical autocorrelation function (acf in R), algorithm on figure 3. see figure 2.

> par(mfrow = c(2,1)) > par(mfrow = c(2,1)) > acf(torus(10ˆ5)) > plot(SFMT(1000, 2)) > acf(torus(10ˆ5, mix=TRUE)) > plot(torus(10ˆ3, 2))

Secondly we compare WELL generator with 4.3 Visual comparisons Faure-Tezuka-scrambled Sobol sequences on fig- ure 4. To understand the difference between pseudo and quasi RNGs, we can make visual comparisons of > par(mfrow = c(2,1)) how random numbers fill the unit square. > plot(WELL(1000, 2)) 1the first 100 000 prime numbers are take from http: > plot(sobol(10ˆ3, 2, scram=2)) //primes.utm.edu/lists/small/millions/. 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 18

SFMT WELL 512a

● ● ● ●● ●● ● ●● ● ● ● ●● ● ● ● ●●●●●● ● ● ● ● ● ●●● ● ●●●●● ●● ● ● ● ● ●● ● 1.0 ● ●● ●● ●●● ● ● ● ● ● ● ●● 1.0 ●● ● ●● ●●●● ● ●● ● ●●●● ● ●● ●● ● ● ● ● ● ● ● ●● ● ● ●●●● ● ●● ●● ● ●● ●● ●● ● ●●● ● ● ●● ●● ●● ●● ●● ● ●●● ● ● ●● ●● ●● ● ●●● ● ● ● ●●● ● ● ● ●●● ● ● ● ● ●● ●●● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ●● ●● ●● ● ● ● ● ● ●● ● ● ● ● ●● ●●●● ●●●● ● ● ● ●●● ● ● ●● ● ● ● ● ●●● ●● ● ●● ●● ● ● ●● ● ● ● ● ●● ● ●● ●● ● ● ● ● ● ● ●● ●● ● ● ● ● ●● ●●● ● ● ● ● ● ● ● ●● ● ●●● ●● ● ● ● ● ● ●● ● ● ● ● ● ●●● ● ● ●●● ● ● ● ● ● ● ●●● ● ● ● ●●● ● ● ● ●● ● ● ● ● ●● ● ●●●● ● ●● ●● ● ●● ● ● ● ●●●● ●● ●●● ● ●● ● ●● ●● ● ● ● ● ● ● ● ● ●●● ● ● ● ●● ● ● ● ● ●● ● ●●● ●●● ●●●● ● ●●● ● ●● ● ● ●● ●●● ● ●● ● ● ●● 0.8 ● ● ● ● ● ● ●● ● ●● ● 0.8 ● ● ● ● ● ●● ● ●●●●● ● ● ● ●● ●● ● ● ● ●●● ● ● ● ● ● ●● ● ● ● ●●● ●● ● ●● ●● ● ●●● ● ● ● ● ● ●● ● ● ● ● ●● ●● ● ● ● ● ● ●● ● ●● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ●●●●● ● ●● ●● ● ● ● ● ●● ● ●●●● ● ● ●●● ● ● ●●●● ●●● ●● ● ● ● ● ●● ● ●● ● ●● ● ● ● ● ●● ● ● ●● ● ● ● ●● ● ● ● ●● ●●●●● ● ● ●● ● ● ● ● ● ● ●●●●● ● ● ●●● ● ● ● ●● ● ●●● ● ● ● ● ● ●●● ●●●● ● ● ● ● ●● ● ● ● ● ● ● ●●● ●●●● ●● ●● ● ● ●●●● ● ● ● ● ● ● ● ●●● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ●● ● ● ● ● ● ●●●● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ●● ● ●● ●● ● ● ●● ●●● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● 0.6 ● ●● 0.6 ● ●●●● ●●●● ● ● ● ●●● ● ● ● ●● ● ● ●●● ● ● ● ●● ●● ● ● ● ●● ● ● ● ●●● ●● ●● ●●● ● ●● ●● ●● ● ● ● ●● ● ● ● ●●● ● ●● ●●●●● ● ●● ● ● ● ● ●●● ● ●● ● ●● ● ● ● ●● ●● ●● ● ● ● ● ●●●● ●● ● ● ●●● ● ●●●●● ● ●● ● ●● ● ●● ●● ● ●● ● ●● ● ● ● ● ● v ● v ● ● ●●●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ●● ●●● ●● ● ● ● ● ● ● ●● ● ● ● ● ●● ●●●● ● ● ● ● ● ●● ●● ● ●●● ●● ● ●●● ● ●●●● ●●●●● ● ● ● ● ● ● ●●●●●● ● ● ● ●●● ●● ●●● ● ● ● ● ● ● ● ●● ● ●● ●● ● ● ● ●● ● ●● ● ●●● ●● ●● ●● ●● ● ●● ● ● ●● ● ● ● ● ●● ●● ● ●● ● ● ● ● ●●●● ● ● ●●● ●●● ● ● ● ● ● ● ● ● ● ●● ● ● ●●● ● ● ● ● ● ●● ● ● ●● ●●● ● ● ● ●● ● ● ● ●●● ● ●● ● ●● ● ● ● ●●●

0.4 ● ● ●● ● ● ●● ● ● ● ● ● ●● ●● 0.4 ● ●● ● ● ● ●●● ● ● ● ● ● ● ●●● ● ● ●●● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ●● ●● ●● ● ●● ● ●● ● ●● ●●● ●●●●●● ● ●● ●● ● ● ●●● ●● ●● ● ●●● ●● ● ● ●● ●● ● ●●●● ● ● ● ●● ● ● ● ●●● ●● ● ● ● ● ●●●●● ● ●● ● ● ●● ● ●● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●●● ●● ●● ●●● ● ● ● ● ● ●●●●● ● ●● ● ●● ● ●● ● ●● ● ● ●●● ● ●●●● ●● ● ●● ● ●● ●● ●● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ●● ●●● ● ● ● ● ● ● ● ● ●● ●● ●● ● ●● ●● ●● ● ● ● ● ●●● ● ●●● ● ● ●● ● ●●● ●● ●● ● ● ● ● ●● ● ●●●● ● ●● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ●●● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ●●●● ● ●●●●● ● ● ●● ● ● ● 0.2 ● ●● ● ● ● ● ● ● ● ●●● 0.2 ● ● ● ● ● ●● ● ●● ● ● ●● ● ●●● ●● ● ● ● ● ●●● ● ● ● ● ● ●● ● ● ●●●●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●●● ● ●● ● ● ●● ● ● ● ● ● ● ●●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ●●● ●● ● ● ● ● ●● ●●● ●● ● ●● ● ● ●●● ● ●●● ●● ● ● ●● ● ● ●●● ● ● ● ● ● ● ● ● ●● ●● ● ● ●● ● ● ● ●●●● ● ●●● ● ● ● ● ● ●●● ● ● ● ●● ●● ● ● ● ●● ● ● ● ●● ●●●● ●● ●● ● ● ● ● ●● ● ● ● ●● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ●●● ● ● ● ● ● ●●● ● ● ● ● ●●● ● ●●●● ● ● ● ● ●● ●●● ● ● ● ● ● ● ● ● ● ● ●●● ● ●● ●● ● ● ● ●● ●● ● ● ● ●●● ● ●● ● ●● ● 0.0 0.0

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

u u

Torus Sobol (Faure−Tezuka)

● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ●● ●● ● ● ●● ● ● ●● ● ● ● ● ● ● ● ●● ●

● 1.0 1.0 ● ● ● ●● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ●● ● ● ●● ● ● ●● ● ● ●●● ● ● ● ● ● ●● ●● ● ● ● ● ● ●● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●●● ●● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ●● ● ● ● ●● ● ● ● ●● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ●●● ●● ● ●● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ●● ● ●●●● ● ●● ● ●●●● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ●● ● ● ●● ● ●● ● ●● ●● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ●●● ● ●● ● ● ● ●●● ● ● ● ●● ●● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ●● ● ● ● ●●● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● 0.8 0.8 ● ● ●● ●● ● ● ● ● ● ● ●● ●● ● ● ● ● ●● ●● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ●● ● ● ●●● ● ● ●●● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ●● ●● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ●● ●● ● ● ● ● ●● ●● ● ● ● ●● ●●● ● ●● ● ● ●●● ●● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ●● ● ● ● ● ●● ●●● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ●● ●● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ●● ● ●● ● ● ●● ● ● ●●● ● ● ●● ● ●● ●● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ●● ●● ● ●

● 0.6 ● 0.6 ● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ●●● ●● ●● ●● ●●● ●● ●●● ● ● ● ●● ● ● ● ● ● ● ●● ●● ● ● ● ●● ●● ● ● ●● ●● ● ●● ● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ●● ● ● ●● ● ● ● v ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● v ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ●● ●● ● ●● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ●● ● ●● ● ● ●● ●● ● ● ●● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ●●● ●● ●●● ●● ●● ●● ●●● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ●● ●● ● ●● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ●● ●● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ● ● ●●● ● ●

● 0.4 ● 0.4 ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ●● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ●● ● ●●● ● ● ●●● ●●● ●●● ● ●●● ● ●● ● ● ● ● ●● ●● ●● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ●● ●● ● ● ● ●● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ●● ●● ● ●● ● ● ●●● ● ●● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ●●● ● ● ●●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ●●● ● ● ●●● ● ● ●●● ● ●●● ● ● ● 0.2 ● 0.2 ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ●● ●● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ●● ● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ●● ● ● ●● ● ● ●● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ● ● ●● ●● ● ● ●● ● ● ● ● ● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ●●● ●● ●● ●● ●● ●●● ●● ● ●● ● ●● ●● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ●● ●● ● ● ● ● ●● ● ● ● ● ●● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ●● ●● ● ● ● ● ●● ● ● ●● ● ●● ●● ● ●● ● ● ● ● ● ● ● ● 0.0 0.0

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

u u

Figure 3: SFMT vs. Torus algorithm Figure 4: WELL vs. Sobol

4.4 Applications of QMC methods defined on the unit hypercube. We want compute Z ||x||2 4.4.1 d dimensional integration Icos(d) = cos(||x||)e dx Rd v  d/2 n u d π X uX −1 2 ≈ cos t (Φ ) (tij) Now we will show how to use low-discrepancy n   sequences to compute a d-dimensional integral i=1 j=1 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 19 where Φ−1 denotes the of the a vanilla European call in the framework of a standard . geometric Brownian motion for the underlying asset. Those options are already implemented in We simply use the following code to com- the package fOptions of Rmetrics bundle1. pute the Icos(25) integral whose exact value is −1356914. The payoff of this classical option is

f(ST ) = (ST − K)+, > I25 <- -1356914 > nb <- c(1200, 14500, 214000) where K is the strike price. A closed formula for > ans <- NULL this call was derived by Black & Scholes (1973). > for(i in 1:3) + { The to price this option + tij <- sobol(nb[i], dim=25, scramb=2, norm=TRUE ) is quite simple + Icos <- mean(cos(sqrt( apply( tijˆ2/2, 1, sum ) ))) * piˆ(25/2) + ans <- rbind(ans, c(n=nb[i], I25=Icos, Delta=(Icos-I25)/I25 )) + } 1. simulate sT,i for i = 1 . . . n from starting > data.frame(ans) point s0, 2. compute the mean of the discounted payoff 1 Pn −rT n I25 Delta n i=1 e (sT,i − K)+. 1 1200 -1355379 -1.131527e-03 2 14500 -1357216 2.223429e-04 3 214000 -1356909 -3.870909e-06 With parameters (S0 = 100, T = 1, r = 5%, K = 60, σ = 20%), we plot the relative error as a function of number of simulation n on figure 5. The results obtained from the Sobol Monte Carlo method in comparison to those obtained by We test two pseudo-random generators (namely Papageorgiou & Traub (2000) with a generalized Park Miller and SF-Mersenne Twister) and one Faure sequence and in comparison with the quasi-random generator (Torus algorithm). No quadrature rules of Namee & Stenger (1967), code will be shown, see the file qmc.R in the Genz (1982) and Patterson (1968) are listed in package source. But we use a step-by-step the following table. simulation for the Brownian motion simulation and the inversion function method for Gaussian n 1200 14500 214000 distribution simulation (default in R). Faure (P&T) 0.001 0.0005 0.00005 Sobol (s=0) 0.02 0.003 0.00006 As showed on figure 5, the convergence of s=1 0.004 0.0002 0.00005 Monte Carlo price for the Torus algorithm is s=2 0.001 0.0002 0.000002 extremely fast. Whereas for SF-Mersenne Twister s=3 0.002 0.0009 0.00003 and Park Miller prices, the convergence is very Quadrature (McN&S) 2 0.75 0.07 slow. G&P 2 0.4 0.06

Table 6: list of errors 4.4.3 Pricing of a DOC

4.4.2 Pricing of a Vanilla Call Now, we want to price a barrier option: a down-out call i.e. an Downward knock-Out In this sub-section, we will present one financial application of QMC methods. We want to price 1created by Wuertz et al. (2007b). 4 DESCRIPTION OF THE RANDOM GENERATION FUNCTIONS 20

where n is the simulation number, d the point Vanilla Call number for the grid of time and Di the opposite SFMT 0.02 Torus of boolean Di. Park Miller zero

0.01 In the following, we set T = 1, r = 5%, st0 = 100, H = K = 50, d = 250 and σ = 20%. We test crude Monte Carlo methods with Park Miller 0.00

relative error and SF-Mersenne Twister generators and a quasi- Monte Carlo method with (multidimensional) -0.01 Torus algorithm on the figure 6. -0.02

0e+00 2e+04 4e+04 6e+04 8e+04 1e+05 Down Out Call

simulation number SFMT 0.02 Torus Park Miller zero

Figure 5: Error function for Vanilla call 0.01

1 Call . These kind of options belongs to the path- 0.00 dependent option family, i.e. we need to simulate relative error

whole trajectories of the underlying asset S on -0.01 [0,T ]. -0.02 In the same framework of a geometric Brow- 0e+00 2e+04 4e+04 6e+04 8e+04 1e+05 simulation number nian motion, there exists a closed formula for DOCs (see Rubinstein & Reiner (1991)). Those options are already implemented in the package Figure 6: Error function for Down Out Call fExoticOptions of Rmetrics bundle2. One may wonder why the Torus algorithm is The payoff of a DOC option is still the best (on this example). We use the d-

f(ST ) = (ST − K)+11(τH >T ), dimensional Torus sequence. Thus for time tj, the simulated underlying assets (s ) are computed where K is the strike price, T the maturity, τ √ tj ,i i H with the sequence (i{ p }) . Thanks to the the stopping time associated with the barrier H j i linear independence of the Torus sequence over and S the underlying asset at time t. t the rationals3, we guarantee a non-correlation of Torus quasi-random numbers. As the price is needed on the whole period [0,T ], we produc as follows: However, these results do not prove the Torus algorithm is always better than traditional Monte

1. start from point st0 , Carlo. The results are sensitive to the barrier level 2. for simulation i = 1 . . . n and time index j = H, the strike price X (being in or out the money 1 . . . d has a strong impact), the asset volatility σ and the time point number d. • simulate stj ,i , • update disactivation boolean Di Actuaries or readers with actuarial background 3. compute the mean of the discounted payoff can find an example of actuarial applications of 1 Pn e−rT (s − K) D , n i=1 T,i + i QMC methods in Albrecher et al. (2003). This 1DOC is disactived when the underlying asset hits the 3 √ √ barrier. i.e. for k 6= j, ∀i, (i{ pj })i and (i{ pk})i are linearly 2 created by Wuertz et al. (2007a). independent over Q. 5 RANDOM GENERATION TESTS 21 article focuses on simulation methods in ruin • and Anderson-Darling statistic is models with non-linear dividend barriers.  2 Z +∞ n(x) − FU (x) dFU (x) 2 F (0,1) (0,1) An = n . −∞ FU(0,1) (x)(1 − FU(0,1) (x)) 5 Random generation tests Those statistics can be evaluated empirically thanks to the sorted sequence of ui’s. But we Tests of random generators aim to check if the will not detail any further those tests, since output u1, . . . , un,... could be considered as according to L’Ecuyer & Simard (2007) they are independent and identically distributed (i.i.d.) not powerful for random generation testing. uniform variates for a given confidence level. There are two kinds of tests of the uniform distribution: first on the interval ]0, 1[, second 5.1.2 The gap test on the binary set {0, 1}. In this note, we only describe tests for ]0, 1[ outputs (see L’Ecuyer & Simard (2007) for details about these two kind of The gap test investigates for special patterns in tests). the sequence (ui)1≤i≤n. We take a subset [l, u] ⊂ [0, 1] and compute the ’gap’ variables with Some RNG tests can be two-level tests, i.e. we  1 if l ≤ U ≤ u do not work directly on the RNG output u ’s but G = i i i 0 otherwise. on a function of the output such as the spacings

(coordinate difference of the sorted sample). The probability p that Gi equals to 1 is just the u − l (the Lebesgue measure of the subset). The test computes the length of zero gaps. If we 5.1 Test on one sequence of n numbers denote by nj the number of zero gaps of length j.

The chi-squared statistic of a such test is given 5.1.1 Goodness of Fit by m 2 X (nj − npj) S = , np Goodness of Fit tests compare the empirical j=1 j cumulative distribution function (cdf) of Fn where p = (1 − p)2pj is the probability that the u ’s with a specific distribution (U(0, 1) here). j i length of gaps equals to j; and m the max number The most known test are Kolmogorov-Smirnov, of lengths. In theory m equals to +∞, but in Cr´amer-von Mises and Anderson-Darling tests. pratice, it is a large integer. We fix m to be at They use different norms to quantify the differ- least ence between the empirical cdf Fn and the true cdf F . log(10−1) − 2 log(1 − p) − log(n) U(0,1) , log(p) in order to have lengths whose appearance prob- • Kolmogorov-Smirnov statistic is abilitie is at least 0.1. √ K = n sup (x) − F (x) , n Fn U(0,1) x∈R 5.1.3 The order test • Cr´amer-von Mises statistic is

Z +∞  2 The order test looks for another kind of patterns. W 2 = n (x) − F (x) dF (x), n Fn U(0,1) U(0,1) We test a d-tuple, if its components are ordered −∞ 5 RANDOM GENERATION TESTS 22 equiprobably. For example with d = 3, we should the unit hypercube [0, 1]t. Tests based on multiple have an equal number of vectors (ui, ui+1, ui+2)i sequences partition the unit hypercube into cells such that and compare the number of points in each cell with the expected number.

• ui < ui+1 < ui+2, • ui < ui+2 < ui+1, 5.2.1 The serial test • ui+1 < ui < ui+2, • ui+1 < ui+2 < ui, • ui+2 < ui < ui+1 The most intuitive way to split the unit hypercube • and ui+1 < ui+2 < ui. [0, 1]t into k = dt subcubes. It is achieved by splitting each dimension into d > 1 pieces. The volume (i.e. a probability) of each cell is just 1 . For some d, we have d! possible orderings of k coordinates, which have the same probability to 1 The associated chi-square statistic is defined as appear d! . The chi-squared statistic for the order test for a sequence (ui)1≤i≤n is just m 2 X (Nj − λ) S = , d! λ 1 2 j=1 X (nj − m d! ) S = 1 , m d! n j=1 where Nj denotes the counts and λ = k their expectation. where nj’s are the counts for different orders and n m = d . Computing d! possible orderings has an exponential cost, so in practive d is small. 5.2.2 The collision test

5.1.4 The frequency test The philosophy is still the same: we want to detect some pathological behavior on the unit hypercube [0, 1]t. A collision is defined as when The frequency test works on a serie of ordered a point v = (u , . . . , u ) falls in a cell where contiguous integers (J = [i , . . . , i ] ∩ ). If we i i i+t−1 1 l Z there are already points v ’s. Let us note C the denote by (n ) the sample number of the set j i 1≤i≤n number of collisions I, the expected number of integers equals to j ∈ J is 1 The distribution of collision number C is given × n, by il − i1 + 1 n−c−1 Y k − i 1 which is independent of j. From this, we can P (C = c) = Sn−c, k kc 2 n compute a chi-squared statistic i=0 k l 2 where 2Sn denotes the Stirling number of the X (Card(ni = ij) − m) S = , second kind1 and c = 0, . . . , n − 1. m j=1 But we cannot use this formula for large n since where m = n . d the Stirling number need O(n log(n)) time to be computed. As L’Ecuyer et al. (2002) we use a n 1 Gaussian approximation if λ = k > 32 and n ≥ 5.2 Tests based on multiple sequences 8 1 2 , a Poisson approximation if λ < 32 and the exact formula otherwise. Under the i.i.d. hypothesis, a vector of output 1 k k k−1 they are defined by 2Sn = k × 2Sn−1 + 2Sn−1 and 1 n values ui, . . . , ui+t−1 is uniformly distributed over 2Sn = 2Sn = 1. For example go to wikipedia. 6 DESCRIPTION OF RNG TEST FUNCTIONS 23

The normal approximation assumes C follows 6 Description of RNG test func- a normal distribution with mean m = n − k + k−1 n tions k k and variance very complex (see L’Ecuyer & Simard (2007)). Whereas the Poisson approxi- mation assumes C follows a Poisson distribution In this section, we will give usage examples of n2 of parameter 2k . RNG test functions, in a similar way as section 4 illustrates section 2 - two first sub-sections. The last sub-section focuses on detecting a particular RNG. 5.2.3 The φ-divergence test

> par(mfrow = c(2,1)) There exist generalizations of these tests where > hist(SFMT(10ˆ3), 100) we take a function of counts Nj, which we called > hist(torus(10ˆ3), 100) φ-divergence test. Let f be a real valued function. The test statistic is given by

k−1 6.1 Test on one sequence of n numbers X f(Nj). j=0 Goodness of Fit tests are already imple- mented in R with the function ks.test for We retrieve the collision test with f(x) = (x−1)+ Kolmogorov-Smirnov test and in package adk (x−λ)2 and the serial test with f(x) = λ . Plenty of for Anderson-Darling test. In the following, we statistics can be derived, for example if we want will focus on one-sequence test implemented in to test the number of cells with at least b points, randtoolbox. f(x) = 11(x=b). For other statistics, see L’Ecuyer et al. (2002). 6.1.1 The gap test

5.2.4 The poker test The function gap.test implements the gap test as described in sub-section 5.1.2. By default, lower and upper bound are l = 0 and u = 0.5, The poker test is a test where cells of the unit cube just as below. [0, 1]t do not have the same volume. If we split the unit cube into dt cells, then by regrouping cells with left hand corner having the same number of > gap.test(runif(1000)) distinct coordinates we get the poker test. In a more intuitive way, let us consider a hand of k Gap test cards from k different cards. The probability to have exactly c different cards is chisq stat = 34, df = 10 1 k! , p-value = 0.00022 P (C = c) = Sc, kk (k − c)! 2 k (sample size : 1000) where C is the random number of different cards d and 2Sn the second-kind Stirling numbers. For a demonstration, go to Knuth (2002). length observed freq theoretical freq 6 DESCRIPTION OF RNG TEST FUNCTIONS 24

11 0 0.12 Histogram of SFMT(10^3) If you want l = 1/3 and u = 2/3 with a SFMT sequence, you just type 15 > gap.test(SFMT(1000), 1/3, 2/3) 10

6.1.2 The order test Frequency 5 The Order test is implemented in function order.test for d-uples when d = 2, 3, 4, 5. A

0 typical call is as following

0.0 0.2 0.4 0.6 0.8 1.0

SFMT(10^3) > order.test(runif(4000), d=4)

Histogram of torus(10^3) Order test

chisq stat = 23, df = 23 10 , p-value = 0.46 8

(sample size : 1000) 6 Frequency 4 observed number 39 31 42 53 37 37 51 41 35 44 37 43 51 38 38 39 48 47 2 39 41 33 52 50 34 0

0.0 0.2 0.4 0.6 0.8 1.0 expected number 42

torus(10^3) Let us notice that the sample length must be a multiple of dimension d, see sub-section 5.1.3. 1 105 125 2 47 62 3 29 31 6.1.3 The frequency test 4 27 16 5 11 7.8 6 7 3.9 The frequency test described in sub-section 5.1.4 7 2 2 is just a equi-distribution test in [0, 1] of the 8 2 0.98 generator. We use a sequence integer to partition 9 3 0.49 the unit interval and test counts in each sub- 10 0 0.24 interval. 6 DESCRIPTION OF RNG TEST FUNCTIONS 25

> freq.test(runif(1000), 1:4) expected number 167

Frequency test In newer version, we will add an argument t for the dimension. chisq stat = 7.1, df = 3 , p-value = 0.069 6.2.2 The collision test

(sample size : 1000) The exact distribution of collision number costs a lot of time when sample size and cell number observed number 236 282 227 255 are large (see sub-section 5.2.2). With function coll.test, we do not yet implement the normal approximation. expected number 250 The following example tests Mersenne-Twister algorithm (default in R) and parameters implying 8 6.2 Tests based on multiple sequences the use of the exact distribution (i.e. n < 2 and λ > 1/32).

Let us study the serial test, the collision test and the poker test. > coll.test(runif, 2ˆ7, 2ˆ10, 1)

6.2.1 The serial test Collision test

Defined in sub-section 5.2.1, the serial test focuses chisq stat = 11, df = 16 on the equidistribution of random numbers in the , p-value = 0.78 unit hypercube [0, 1]t. We split each dimension of the unit cube in d equal pieces. Currently in function serial.test, we implement t = 2 and exact distribution d fixed by the user. (sample number : 1000/sample size : 128 / cell number : 1024)

> serial.test(runif(3000), 3) collision observed expected number count count Serial test

1 2 2.3 chisq stat = 4.7, df = 8 2 7 10 , p-value = 0.79 3 24 29 4 55 62 (sample size : 3000) 5 85 102 6 143 138 7 161 156 observed number 176 157 159 178 8 145 151 184 162 161 161 162 9 136 126 6 DESCRIPTION OF RNG TEST FUNCTIONS 26

10 99 93 6.2.3 The poker test 11 60 61 12 45 36 poker.test 13 20 19 Finally the function implements 14 9 8.9 the poker test as described in sub-section 5.2.4. 15 6 3.9 We implement for any “card number” denoted by 16 2 1.5 k. A typical example follows 17 1 0.56 > poker.test(SFMT(10000)) When the cell number is far greater than the sample length, we use the Poisson approximation Poker test (i.e. λ < 1/32). For example with congruRand generator we have chisq stat = 1.2, df = 4 , p-value = 0.88 > coll.test(congruRand, 2ˆ8, 2ˆ14, 1)

(sample size : 10000) Collision test

observed number 3 197 939 787 74 chisq stat = 11, df = 7 , p-value = 0.16 expected number 3.2 192 960 768 77

Poisson approximation (sample number : 1000/sample size : 256 6.3 Hardness of detecting a difference / cell number : 16384) from truly random numbers collision observed expected Random number generators typically have an number count count internal memory of fixed size, whose content is called the internal state. Since the number of possible internal states is finite, the output 0 129 135 sequence is periodic. The length of this period 1 282 271 is an important parametr of the random number 2 265 271 generator. For example, Mersenne-Twister gen- 3 199 180 erator, which is the default in R, has its internal 4 90 90 state stored in 624 unsigned integers of 32 bits 5 24 36 each. So, the internal state consists of 19968 bits, 6 6 12 but only 19937 are used. The period length is 7 5 3.4 219937 − 1, which is a .

Large period is not the only important parame- Note that the Normal approximation is not yet ter of a generator. For a good generator, it is not implemented and those two approximations are computationally easy to distinguish the output of not valid when some expected collision numbers the generator from truly random numbers, if the are below 5. seed or the index in the sequence is not known. 6 DESCRIPTION OF RNG TEST FUNCTIONS 27

Generators, which are good from this point of + M2 <- 30307 view, are used for cryptographic purposes. These + M3 <- 30323 generators are chosen so that there is no known + y <- round(M1*M2*M3*x) procedure, which could distinguish their output + s1 <- y %% M1 from truly random numbers within practically + s2 <- y %% M2 available computation time. For simulations, this + s3 <- y %% M3 requirement is usually relaxed. + s1 <- (171*26478*s1) %% M1 + s2 <- (172*26070*s2) %% M2 However, even for simulation purposes, consid- + s3 <- (170*8037*s3) %% M3 ering the hardness of distinguishing the generated + (s1/M1 + s2/M2 + s3/M3) %% 1 numbers from truly random ones is a good + } measure of the quality of the generator. In > RNGkind("Wichmann-Hill") particular, the well-known empirical tests of > xnew <- runif(1) random number generators such as Diehard1 or > err <- 0 TestU01 L’Ecuyer & Simard (2007) are based on > for (i in 1:1000) { comparing statistics computed for the generator + xold <- xnew with their values expected for truly random + xnew <- runif(1) numbers. Consequently, if a generator fails an + err <- max(err, abs(wh.predict(xold) - xnew)) empirical test, then the output of the test provides + } a way to distinguish the generator from the truly > print(err) random numbers.

Besides of general purpose empirical tests [1] 0 constructed without the knowledge of a concrete generator, there are tests specific to a given gen- The printed error is 0 on some machines and erator, which allow to distinguish this particular less than 5 · 10−16 on other machines. This is generator from truly random numbers. clearly different from the error obtained for truly random numbers, which is close to 1. An example of a generator, whose output may easily be distinguished from truly random The requirement that the output of a random numbers, is the older version of Wichmann-Hill number generator should not be distinguishable from 1982. For this generator, we can even predict from the truly random numbers by a simple the next number in the sequence, if we know computation, is also directly related to the way, the last already generated one. Verifying such a how a generator is used. Typically, we use the predicition is easy and it is, of course, not valid generated numbers as an input to a computation for truly random numbers. Hence, we can easily and we expect that the distribution of the output distinguish the output of the generator from truly (for different seeds or for different starting indices random numbers. An implementation of this test in the sequence) is the same as if the input in R derived from McCullough (2008) is as follows. are truly random numbers. A failure of this assumption implies that observing the output of > wh.predict <- function(x) the computation allows to distinguish the output + { from the generator from truly random numbers. + M1 <- 30269 Hence, we want to use a generator, for which we may expect that the calculations used in the 1 The Marsaglia Random Number CDROM including intended application cannot distinguish its output the Diehard Battery of Tests of Randomness, Research from truly random numbers. Sponsored by the National Science Foundation (Grants DMS-8807976 and DMS-9206972), copyright 1995 George Marsaglia. In this context, it has to be noted that many 7 CALLING THE FUNCTIONS FROM OTHER PACKAGES 28 of the currently used generators for simulations C, call the routine torus,. . . can be distinguished from truly random numbers using the arithmetic mod 2 applied to individual bits of the output numbers. This is true for Using R level functions in a package simply Mersenne Twister, SFMT and also all WELL requires the following two import directives: generators. The basis for tolerating this is based on two facts. Imports: randtoolbox

First, the arithmetic mod 2 and extracting individual bits of real numbers is not directly used in file DESCRIPTION and in typical simulation problems and real valued functions, which represent these operations are extremely discontinuous and such functions also import(randtoolbox) do not typically occur in simulation problems. Another reason is that we need to observe quite a in file NAMESPACE. long history of the output to detect the difference from true randomness. For example, for Mersenne Accessing C level routines further requires to Twister, we need 624 consecutive numbers. prototype the function name and to retrieve its pointer in the package initialization function On the other hand, if we use a cryptographi- R init pkg, where pkg is the name of the cally strong pseudorandom number generator, we package. may avoid a difference from truly random num- bers under any known efficient procedure. Such For example if you want to use torus C generators are typically slower than Mersenne function, you need Twister type generators. The factor of slow down may be, for example, 5. If the simulation problem requires intensive computation besides void (*torus)(double *u, int nb, int dim, the generating random numbers, using slower, but int *prime, int ismixed, int usetime); better, generator may imply only negligible slow down of the computation as a whole. void R_init_pkg(DllInfo *dll) { torus = (void (*) (double, int, int, int, int, int)) \ 7 Calling the functions from R_GetCCallable("randtoolbox", "torus"); other packages }

In this section, we briefly present what to do if See file randtoolbox.h to find headers of you want to use this package in your package. RNGs. Examples of C calls to other functions This section is mainly taken from package expm can be found in this package with the WELL RNG available on R-forge. functions.

Package authors can use facilities from rand- The definitive reference for these matters re- toolbox in two ways: mains the Writing R Extensions manual, page 20 in sub-section “specifying imports exports” and page 64 in sub-section “registering native • call the R level functions (e.g. torus) in R code; routines”.

• if random number generators are needed in REFERENCES 29

References L’Ecuyer, P. (1990), ‘Random numbers for sim- ulation’, Communications of the ACM 33, 85– Albrecher, H., Kainhofer, R. & Tichy, R. E. 98. 2, 3, 4 (2003), ‘Simulation methods in ruin models L’Ecuyer, P. & Simard, R. (2007), ‘Testu01: A with non-linear dividend barriers’, Mathemat- c library for empirical testing of random num- ics and Computers in Simulation 62, 277–287. ber generators’, ACM Trans. on Mathematical 20 Software 33(4), 22. 21, 23, 27 Antonov, I. & Saleev, V. (1979), ‘An economic L’Ecuyer, P., Simard, R. & Wegenkittl, S. method of computing lp sequences’, USSR (2002), ‘Sparse serial tests of uniformity for Comp. Mathematics and Mathematical Physics random number generations’, SIAM Journal on 19 pp. 252–256. 10 scientific computing 24(2), 652–668. 22, 23

Black, F. & Scholes, M. (1973), ‘The pricing of Marsaglia, G. (1994), ‘Some portable very-long- options and corporate liabilities’, Journal of period random number generators’, Computers Political Economy 81(3). 19 in Physics 8, 117–121. 13 Matsumoto, M. & Nishimura, T. (1998), Bratley, P. & Fox, B. (1988), ‘Algorithm 659: ‘Mersenne twister: A 623-dimensionnally Implementing sobol’s quasi-random sequence equidistributed uniform pseudorandom number generators’, ACM Transactions on Mathemati- generator’, ACM Trans. on Modelling and cal Software 14(88-100). 10 Computer Simulation 8(1), 3–30. 4, 6 Bratley, P., Fox, B. & Niederreiter, H. (1992), Matsumoto, M. & Saito, M. (2008), SIMD- ‘Implementation and tests of low discrepancy oriented Fast Mersenne Twister: a 128- sequences’, ACM Transactions Mode; Comput. bit pseudorandom number generator, Monte Simul. 2(195-213). 10 Carlo and Quasi-Monte Carlo Methods 2006, Springer. 6, 15 Eddelbuettel, D. (2007), random: True random numbers using random.org. McCullough, B. D. (2008), ‘Microsoft excel’s URL: http://www.random.org 2 ‘not the wichmann–hill’ random number gen- erators’, Computational Statistics and Data Genz, A. (1982), ‘A lagrange extrapolation al- Analysis 52, 4587–4593. 11, 27 gorithm for sequences of approximations to multiple integrals’, SIAM Journal on scientific Namee, J. M. & Stenger, F. (1967), ‘Construction computing 3, 160–172. 19 of ful ly symmetric numerical integration for- mulas’, Numerical Mathatematics 10, 327–344. Hong, H. & Hickernell, F. (2001), Implementing 19 scrambled digital sequences. preprint. 10 Niederreiter, H. (1978), ‘Quasi-monte carlo meth- ods and pseudo-random numbers’, Bulletin of J¨ackel, P. (2002), Monte Carlo methods in finace, the American Mathematical Society 84(6). 2, John Wiley & Sons. 7 7, 8, 9, 11 Joe, S. & Kuo, F. (1999), Remark on algorithm Niederreiter, H. (1992), Random Number Gener- 659: Implementing sobol’s quasi-random se- ation and Quasi-Monte Carlo Methods, SIAM, quence generator. Preprint. 10 Philadelphia. 7 Knuth, D. E. (2002), The Art of Computer Pro- Panneton, F., L’Ecuyer, P. & Matsumoto, M. gramming: seminumerical algorithms, Vol. 2, (2006), ‘Improved long-period generators based 3rd edition edn, Massachusetts: Addison- on linear recurrences modulo 2’, ACM Trans. Wesley. 3, 4, 13, 23 on Mathematical Software 32(1), 1–16. 5 REFERENCES 30

Papageorgiou, A. & Traub, J. (2000), ‘Faster evaluation of multidimensional integrals’, arXiv:physics/0011053v1 p. 10. 19

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, 13

Patterson, T. (1968), ‘The optimum addition of points to quadrature formulae’, Mathematics of Computation pp. 847–856. 19

Planchet, F., Th´erond,P. & Jacquemin, J. (2005), Mod`elesFinanciers En Assurance, Economica. 14

Press, W., Teukolsky, W., William, T. & Brian, P. (1996), Numerical Recipes in Fortran, Cam- bridge University Press. 10

Rubinstein, M. & Reiner, E. (1991), ‘Unscram- bling the binary code’, Risk Magazine 4(9). 20

Wichmann, B. A. & Hill, I. D. (1982), ‘Algorithm as 183: An efficient and portable pseudo- random number generator’, Applied Statistics 31, 188–190. 13

Wuertz, D., many others & see the SOURCE file (2007a), fExoticOptions: Rmetrics - Exotic Option Valuation. URL: http://www.rmetrics.org 20

Wuertz, D., many others & see the SOURCE file (2007b), fOptions: Rmetrics - of Option Valuation. URL: http://www.rmetrics.org 19