RNG Revised
RNG Revised
Roberto Innocente
SISSA, Trieste
for the joint ICTP/SISSA MHPC.it master course
cos(θ) π
Z π
sin(θ)
Shaded Portion Area = dθ = − =1
θ=0 2 2 0
# R code
buffonp <- function ( n ) {
k =0;
for ( i in 1: n ) {
theta = runif (1 , min =0 , max = pi )
y = runif (1 , min =0 , max =1/2) ;
if ( y +1/2* sin ( theta ) >1/2) k = k + 1 }
return ( k / n )
}
for ( i in 1:6) {
w =10^ i ; bp = buffonp ( w )
cat ( ’ rn = ’ ,w , ’ computed pi = ’ ,2.0/ bp , ’ error = ’ , abs ( pi -2.0/ bp
) , ’\n ’)
}
# rn = 10 computed pi = 2 error = 1.141593
# rn = 100 computed pi = 3.389831 error = 0.2482379
# rn = 1000 computed pi = 3.04878 error = 0.09281217
# rn = 10000 computed pi = 3.134305 error = 0.007287686
# rn = 1 e +05 computed pi = 3.138584 error = 0.003008469
# rn = 1 e +06 computed pi = 3.144595 error = 0.003002102
plot (x ,y , type = ’n ’)
title ( ’ Exponential distr : l * exp ( - l * x
0.8
Exponential CDF ) ’)
lines (x ,y , col = ’ red ’) ; lines (x ,z , col
= ’ green ’)
0.6
green ’)
text (2 ,0.1 , ’ Exponential pdf ’ , col = ’
red ’)
0.2
Exponential pdf
invcdf < - function ( yy ) { lambda = 1;
xx = - log (1 - yy ) / lambda ;
0.0
0 1 2 3 4 5
return ( xx ) ; }
w = runif (1000) ; ic = invcdf ( w )
x
lines ( ecdf ( ic ) , xlim = c (0 ,5) , ylim = c
(0 ,1) )
S
initial state
Set of states
s0=initial state
g:S-->(0,1) f:S-->S
output transition
function function
(0,1)
There can be confusion about these terms, they refer to the general
case in which a generator can cycle skipping some initial outcomes.
LCG (m, a, c, x0 ) :
An LCG has full period m if and only if :
1 The GCD(Greatest Common Divisor) of m and c is 1.
2 if q is a prime that divides m then q divides (a − 1).
3 if 4 divides m, then 4 divides (a − 1)
(Hull-Dobell Theorem)
were used extensively, but in the ' 90 it was discovered that they
fail a famous test of randomness (but a workaround exists). The
one proposed by Marsaglia is xn ≡ xn−5 + xn−17 (mod 2k ). Period
of this lagged Fibonacci is 2k ∗ (217 − 1), quite longer than LCGs..
State is an array of 17 integers.
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
60000
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ●● ● ●● ●
● ● ● ● ● ● ● ● ●● ● ● ●●
● ●● ● ●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ●
●●
●● ●● ● ● ● ● ●●
● ● ● ● ● ● ● ●● ●● ●● ● ●● ● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ●●
●●
●●
●● ●● ●●
●● ●● ● ● ● ●● ●●
● ● ● ● ● ● ● ●● ●● ●● ●● ●● ● ● ● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●●
50000
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ● ● ● ●
● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ●
● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●●● ●● ●● ●● ●●● ●●● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ●● ● ● ● ●● ●● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ●●● ● ●● ●
●● ●●● ●● ● ●●● ●
●● ●
● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●
●● ● ●●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ● ●● ●● ● ●● ●● ●● ●● ●● ●● ●● ●●
● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●●●● ● ●● ●● ●● ●● ●● ●● ●●
●● ●● ● ●●
40000
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ● ●● ●● ●● ●● ●● ●● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ● ● ● ●● ● ●● ● ● ● ● ●● ● ● ● ● ●
●● ●● ●● ●● ●● ●● ●●● ●●● ●● ● ● ●● ●
●● ●● ●● ●●● ●●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ●●●● ●● ●● ●● ●● ●● ●● ●● ● ● ●
● ●● ●● ●● ●● ●● ●● ●●
● ● ● ● ● ● ● ●● ●● ●● ●● ● ●● ●● ●● ●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ●●
●●
●●
●● ●● ●● ●●
●●
●●
●● ●● ●● ●●
●●
● ●
●●
● ●● ●● ●●
●●
●●
●●
●●
●● ●● ● ●●
● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●●
● ●● ●● ●● ●● ●● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●
● ●● ●● ●● ●● ●● ●● ● ●●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ● ●●● ●● ●● ●● ●● ●● ●●
● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●●●● ●● ●● ●● ●● ●● ●● ●● ● ●
● ●● ●● ●● ●●
●● ●● ● ●●
● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ● ●● ●● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ●● ● ●● ● ●●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●●● ● ●● ●● ●● ● ● ● ● ●● ● ● ● ● ●● ●● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ●●●● ●● ● ●● ●● ● ● ●●● ●●● ● ● ●
●● ●
●● ● ● ●
● ● ● ●●● ●●
● ● ● ● ● ● ● ●●●● ●● ● ●● ● ●● ●●
●●
●●
●●
●●
●● ●● ●● ●●
●●
●●
●● ●● ●● ●●
●● ●●
●●
● ●● ● ●● 70000
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ●● ● ●● ●● ●● ●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ●● ● ● ●
●● ●
●● ●● ●
●● ● ●
●● ●●● ●● ● ●
●● ● ●
●● ●●● ●
●● ●
●● ● ●
●● ● ●
●● ● ● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ●● ●● ●● ● 60000
● ● ● ● ● ● ● ● ● ● ● ● ●●
●●
●●
●● ●● ● ●● ● ● ● ●● ●
●● ●
●● ●● ●● ●
●● ●
●● ●
●●
● ●● ● ●● ●
● ● ● ● ● ● ● ●● ● ● ● ● ●
●● ● ●
● ●● ●●● ● ●
●● ●
●● ●●● ● ●● ●
●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ● ●● ● ●● ●● ●● ●● ●● ●● ● ●●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ●● ●●
●●
●●
●● ●● ● ●● ● ● ●
●● ●
●●
● ●● ●● ●
●● ●
●●
●●● 50000
● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ●● ● ● ● ● ● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ●● ●●● ●● ●● ●● ● ●●
10000
●● ●● ●● ●● ●● ●● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●
●●
●●
●●
●●
●● ●● ● ●● ● ● ● ●● ●
●● ●
●●● ● ● ● 40000
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ● ●● ●● ● ● ● ● ●● ●●● ●●
● ● ● ● ● ● ● ●● ●● ●● ●● ●● ● ● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ●● ●●
●●
●●
●●
●●
●● ●● ● ●● ● ●●
● ●
●
● ●● 30000
● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●● ● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ●● ●● ●
● ● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ●●
● ●
●●
●●
●●
●● ●● ● ●● ● 20000
● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●● ● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ●●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●
● ● ●● ● ●● ●
● ● ● ● ● ● ● ● ● ● ● ● ●●
● ● ● ●● ● 10000
● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
0
● ● ● ● ● ● ● ● ● ● 0
0
AWC
x [ i ] = ( x [i - r ] + x [i - s ]+ c [i -1]) % m ;
c [ i ] = ( x [i - r ]+ x [i - s ]+ c [i -1]) / m ;
SWB
#define SWB (c++, bro=(x<y),t[c]=(x=t[UC (c+34)])-(y=t[UC (c+19)]+bro))
[13]
MRG:
xi = a1 xi−1 + . . . + ak xi−k (mod m) , i ≥ k
where m and k are positive integers called modulus and order and
the coefficients a1 , . . . , ak are in Zm . The state at step i is
si = (xi−k+1 , . . . , xi )T (a vector of length k). The initial state s0 is
required to be different from all 0. When m = p is a prime number
the ring Zp is a finite field and it is possible to choose the aj in
such a way that the period reaches ρ = p k − 1 (Knuth, 1998).
This maximal period is achieved iff the characteristic polynomial of
the recurrence P(z) = z k − a1 z k−1 − . . . − ak is a primitive
polynomial. Alanen and Knuth gave 3 conditions for verifying the
primitivity of P(z). In addition, a maximum-period MRG is known
to be equidistributed up to k-dimensions : every t-uple of Zp
Xt
Ut =
m
where A is an invertible k × k matrix and Xt is a k × 1 vector.
They were a great advance in the RNG arena. Here the heuristic is
that combining generators maybe of not so good quality for today
standard and shuffling, adding or selecting could make a better
generator. One class that was thoroughly studied was that of
Combined MRG. In some cases the theory can predict the period.
Methods used:
Add rn from 2 or more generators. If xi and yi are sequences
in [0..(m − 1)] then xi + yi (mod ()m) is also a sequence in
[0..(m − 1)].
XOR rn from 2 or more generators (Santa, Vazirani 1984)
Shuffle with a rn generator xi the output from another rn
generator yi (Marsaglia, Bray 1964) (e.g. keep last 100 items
from sequence yi use xi to choose from this buffer.
xn = a1 xn−1 + . . . + ak xn−k
un = xn /m
wherem and k are positive integers and each ai belongs to Zm .
This recurrence has maximal period length mk − 1 attained iff m is
prime and the characteristic polynomial
P(z) = zk − a1 z k−1 . . . − ak is primitive. The last condition to
avoid too many computations can often be achieved with only 2
non zero coefficients like ar and ak with 1 <= r < k. If we have L
MRGs ∀l | 0 ≤ l < L − 1 :
L
X
zn = dl xl,n (mod m1 )
l=1
un = zn /m1
then wn is exactly equivalent to an MRG with modulus
m = m1 m2 . . . mL . (L'Ecuyer 1998).
In MATLAB/Octave :
N =100; % compute N rn
U = zeros (1 , N ) ;
for t :1: N
Xt = mod ( ax2p * X (2) - ax3n * X (3) , m1 ) ;
Yt = mod ( ay1p * Y (1) - ay3n * Y (3) , m2 ) ;
if Xt <= Yt
U ( t ) =( Xt - Yt + m1 ) /( m1 +1) ;
else
U ( t ) =( Xt - Yt ) /( m1 +1) ;
end
X (2:3) = X (1:2) ; X (1) = Xt ; Y (2:3) = Y (1:2) ; Y (1) = Yt ;
end
Knuth says: all good rng pass this test, all bad fail it : it is a very
important test. Usually the set of overlapping vectors :
Irreducible/Primitive
trinomials
x m + x k + 1 over F2 .
Handbook of Applied
Cryptography,
Menezes ( [10] )
There are two ways to get random integers or floats from an LFSR
random bit generator:
run an LFSR generator l times and get l bits from it and
consider them as the binary fraction of a float in (0, 1) or an
integer [0..(2l − 1)].
x = (b0 , . . . , bl−1 )T produced by a LFSR ( usually based on a
trinomial)
bi = bi−p ⊕ bi−p+q
are taken to represent the fraction of a float or an integer with
l bits ( l-wise decimation of the sequence of bi ). If l is
relatively prime with 2p − 1 (the period of the LFSR), also the
xi = xi−p ⊕ xi−p+q
xp + xr + 1
Tausworthe (1965)
An RNG built on this is sometimes indicated as R(r, p)
l−1
X
Un = bnl+j 2−j−1
j=0
xn = xn−p ⊕ xn−p+q
It has period (2p − 1). The initial values are p integers. See
next picture.
Matsumoto [27]
Pros of GFSR : fast generation of rn, sequence has arbitrarily long
period, implementation does not depend on word size.
Cons : selection of seeds its critical and good initialization is time
consuming, period 2p − 1 is quite smaller than the storage area
would allow.
xl+n = xl+m ⊕ xl A , (l = 0, 1, . . .)
where A is a w × w matrix with 0, 1 components. With suitable
choices of n, m, A the TGFSR generator attains the maximal
period of 2nw − 1. Because it has maximal period it is
n − equidistributed. The trick is simply to update xl with a twist :
xn = bk (n)
Code in R :
# hypothesis testing
xmi =0.2; xma =0.8; xl =0.45
x = seq ( xmi , xma ,0.01)
xmean =1/2;
N =30
sigma =1/ sqrt (12) ; stdev = sigma / sqrt ( N )
plot (x , dnorm (x , m = xmean , sd = stdev ) , type = ’n ’)
title ( ’ Test of hypothesis : H0 ~ N (1/2 , sd = sigma / sqrt ( N )
) ’)
str = bquote ( H_0 ~ N (1/2 ,1/ sqrt (12*50) ) )
lines (x , dnorm (x , m = xmean , sd = stdev ) , col = ’ red ’)
text (0.5 ,6.5 , ’ H0 ~ N (1/2 , sd = sigma / sqrt ( N ) ) ’, col = ’ red ’)
xx = c ( xmi -0.01 , seq ( xmi , xl ,0.01) , xl )
yy = c (0 , dnorm ( seq ( xmi , xl ,0.01) ,m = xmean , sd = stdev ) ,0)
polygon ( xx , yy , col = ’ skyblue ’)
N (µ, sd =
p
1/12/sqrt(n)).
the data.
√ √
s X N Ns − k k N − k N−k−1
P(kN± ≤ s) = √ (−1) k
1+
N √ k N N
0≤k≤b Nsc
TestU01 contains over 200 predefined rng for test purposes (LCG, MRG,
combined MRG, lagged-Fibonacci, AWC, SWB, MWC LFSR, combined
LFSR, GFSR, twisted GFSR, Mersenne twisters, WELL, . . . ). It divides
its tests for U(0, 1) variates in 3 batteries (SmallCrush, Crush[96 tests],
BigCrush[106 tests]) of increasing time and complexity:
e.g. exec time respectively 14 sec(SmallCrash), 1 hour(Crush), 5.5
hours(BigCrush).
Small Crush battery of tests :
1 smrsa BirthdaySpacings 6 sknuth MaxOft
2 sknuth Collision 7 svaria WeightDistrib
3 sknuth Gap 8 smarsa MatrixRank
4 sknuth SimpPoker 9 sstring HammingIndep
5 sknuth CouponCollector swalk RandomWalk1
10
http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html
Usage : ./assess <length_of_rn>
Parameter Value
n 624
w 32
r 31
m 397
Period 2k − 1 with k = 32, 64, 96, 128, 160, 192. Produces integers
∈ [0 . . . 232 − 1], by means of the XOR instruction. In C : y^(y<<a),
in Fortran : IEOR(y,ishft(y,a)). To give an idea of the power of this
procedure, given 4 32 bits seeds x, y , z, w the sequence :
tmp =( x ^( x < <15) ) ;
x=y;y=z;z=w;
return w =( w ^( w > >21) ) ^( tmp ^( tmp > >4) ) ;
In MATLAB/Octave :
Main program in C :
# include < stdio .h >
int main ( void ) {
int i ; UL k ;
settable (12345 ,65435 ,34221 ,12345 ,9983651 ,95746118) ;
/* Global static v a r i a b l e s : */
static UL z =362436069 , w =521288629 , jsr =123456789 , jcong
=380116160;
static UL a =224466889 , b =7584631 , t [256] , x =0 , y =0 , bro ;
static unsigned char c =0;
void settable ( UL i1 , UL i2 , UL i3 , UL i4 , UL i5 , UL i6 )
{ int i ; z = i1 ; w = i2 ; jsr = i3 ; jcong = i4 ; a = i5 ; b = i6 ;
for ( i =0; i <256; i = i +1) t [ i ]= KISS ; }
2 SHR
2 shift register generators : F (250, 103, ⊕), F (1279, 1063, ⊕)
SWC
A subtract with carry generator: F (1279, 1063, −)
SWCW
A combined subtract with carry and Weyl generator
A side argument :
quasi-random numbers (or sub-random)
quasi-Monte Carlo
Uses instead of random numbers low discrepancy numbers (or
quasi-random). Can converge faster if function is smooth.
Ran.h
Implement Numerical Recipes Ran and test it with dieharder ,
[11] Lehmer, D. H.
Mathematical methods in large-scale computing units
Proceedings of a Second Symposium on Large-Scale Digital
Calculating Machinery: 141146. MR 0044899. 1949.
journal : Annals of the Computation Laboratory of Harvard
University, Vol. 26 (1951)).
[12] G. Marsaglia.
Random numbers fall mainly in the planes.
PNAS, 61 (1): 2528. 1968.
[13] A new class of Random Number generators (AWC,SWB)
G.Marsaglia, Zaman
The Annals of Applied Probability, 1991, n. 3
E N D