You are on page 1of 12

Generation of Uniform (0,1) Random Numbers

Introduction to random numbers
A random number is a number generated by a process, whose outcome is unpredictable, and which cannot be subsequentially reproduced. This works fine provided that one has some kind of a black box which will fulfill this task and such a black box can be called as a random number generator. The sequence of number is random if the quantity of information it contains – in the sense of Shannon's information theory – is also infinite. In other words, it must not be possible for a computer program, whose length is finite, to produce this sequence. A concept that is present in both of these definitions and that must be emphasized is the fact that numbers in a random sequence must not be correlated. Random simulation has long been a very popular and well researched field of mathematics. There exists a wide range of applications in biology, insurance, physics and many other fields of applied science. So simulations of random numbers are crucial. Let us recall the only things, that are truly random, are the measurement of physical phenomena such as thermal noises of semiconductor chips or radioactive sources. The only way to simulate some randomness on computers are carried out by deterministic algorithms. Thus the concept of truly randomness doesn’t exist through a computer algorithm. Excluding true randomness, there are two kinds random generation: pseudo and quasi random number generators.

Overview of random number generation
Firstly, we present the pseudo random number generation and then the quasi random number generation. Random numbers are known as random variates of the uniform U(0; 1) distribution. More complex distributions can 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 deterministic but well equidistributed. Statistical randomness tests aim at determining whether a particular sequence of numbers was produced by a random number generator. The approach is to calculate certain statistical quantities and compare them with average values that would be obtained in the case of a random sequence. These average values are obtained from calculations performed on the model of an ideal random number generator. Testing randomness is an empirical task. There exists numerous tests, each one of them revealing a particular type of imperfection in a sequence.

Pseudo random generation

uniform random variates. Japenese mathematicians Matsumoto and Nishimura invented the first algorithm in 1998 whose period (219937-1) exceeds the number of electron spin changes since the creation of the Universe (106000 against 10120). So. The period of a RNG. there were no perfect algorithms to generate pseudo random numbers. : : : . f. si = f(si=1) and ui =g(si). called the initial distribution. As described in L'Ecuyer (1990). Generally. U. a finite set of output symbols U. a transition function f : S S. . The article of Park & Miller (1988) entitled Random generators: good ones are hard to find and is a clear proof. Most of the users thought the rand function they used was good because of a short period and a term to term dependence. seems real (independent and identical) i. The properties of Pseudo random numbers are  Low storage requirement: .At the start of the nineties. can all the algorithms can be accepted as generator of random numbers. 2. un. It was a big breakthrough. an output function g : S U. the seed s0 is determined using the clock machine. is the smallest integer p € N. This basically explains that the algorithm should be logical and should follow sequential methodology. generate the initial state (called the seed) s0 according to µ and compute u0 = g(s0). The requirement of a random number is that the numbers should meet ―all‖ aims. Then the generation of random numbers is as follows: 1. such that ¥n € N.i. What are the good random generator algorithms which produces good random numbers? How do we say that a particular random number is good to be accepted? There are certain qualities based on which we can say that a particular random number is good. iterate for i = 1. µ is a probability distribution on S. or rather pass as many tests as possible. µ. Qualities of random generators The random numbers can be generated by variety of ways. and so the random variates u0…. g) where • • • • • S is a finite set of states.The random number and its algorithm should not require extensive memory in order to run the program. a (pseudo) random number generator (RNG) is defined by Structure (S. sp+n = sn.d. a key characteristic.

For example.    Speed: . A second group of requirements are the statistical tests that check whether the numbers are distributed as intended. The third group of tests is to check how well the random numbers distribute in higher-dimensional spaces.The rate of generating random number should be high to fulfill requirement of problem. 2. m – 1 }. The random number generator should be able to pass certain tests in order to be called as the good generator of random number. This leads to select M close to the largest integer machine number. and compares to the desired values of μ and σ2. Un = Xn/m Use odd number as seed Algebra/group theory helps with choice of a Want cycle of generator (number of steps before it begins repeating) to be large Don't generate more than m/1000 numbers Composite generator Xn+1 = a1 Xn + c1 (mod m) .) Another simple test is to check correlations. Replicability: . Repeatability: . because a small set of numbers makes the outcome easier to predict —a contrast to randomness. …. The simplest of such tests evaluates the sample mean ˆμ and the sample variance ˆs2 of the calculated random variates.The repeatability should occur after a long period of time.The algorithm should provide the feature of replicability in order to find the variability among the random number generation by changing seeds. These tests can be broadly defined under three groups: The first requirement is that of a large period. The number M must be as large as possible. it would not be desirable if small numbers are likely to be followed by small numbers. Long period: .It should be repeatable after a period of time. Generating uniform (0. (Recall μ = 1/2 and σ2 = 1/12 for the uniform distribution. 1.1) random number using Linear congruential generator(LCG) Xn+1 = a Xn + c (mod m) m = modulus = 232 – 1 a = multiplier = choose carefully c = increment = (maybe) 0 X0 = seed • • • • • Xn €{ 0.

. i = 1. X0. ..X1 are seeds. where Ri = Xi/m. and a = b mod(m) if a is the remainder of b/m. . Also. it’s not possible to get Xi−1 < Xi+1 < Xi or Xi < Xi+1 < Xi−1 . Problem: Small numbers follow small numbers. . 6 = 13mod 7.g. m is the modulus. e. 2.Yn+1 = a2 Yn + c2 (mod m) Wn+1 = Xn + Yn (mod m) Shuffling a random number generator • Initialization – – • Generate an array R of n (= 100) random numbers from sequence Xk Generate an additional number X to start the process Each time generator is called – – – – Use X to find an index into the array R j ¬ X * n X ¬ R[ j ] R[ j ] ¬ a new random number Return X as random number for call • • Shuffling with two generators Initialization – Generate an array R of n (= 100) random numbers from sequence Xk • Each time generator is called – – Generate X from Xk and Y from Yk Use Y to find an index into the array R • j¬Y*n – – – Z ¬ R[ j ] R[ j ] ¬ X Return Z as random number for call Fibonacci Generators Fibonacci and Additive Congruential Generators Take Xi = (Xi−1 + Xi−2)mod(m).

Let X be a random variable with C.U17 with a congruential generator. set j := 17 Initialization: Set i = 17. Using the inverse and hence the name Inverse Transform Method..D.. b = 150889. then . The Central Limit Theorem assures that X is approximately normally distributed. by a suitable transformation out of a uniformly distributed random variable but the most obvious approach inverts the distribution function. 1). and calculate U1. But this crude attempt is not satisfying. j = 5. Inversion Transform Method Generate a continuous random variable X ∼ F as follows:   Generate a random uniform variable U Set X = 1/F(U) Assumption is that F-1(X) exists. the inverse function FX-1 (y) may be defined for any value of y between 0 and 1 as: FX-1 (y) = inf{ x: FX (x) >= y} 0<= y <= 1 FX-1 (y) is defined to equal that value x for which F(x)=y. Better methods calculate non uniformly distributed random variables. Set the seed N0 = your favorite dream number. for example. Since FX (x) is a nondecreasing function. . FX (x). Let us proof that if U is uniformly distributed over the interval (0. possibly inspired by the system clock of your computer.Algorithm for a typical Fibonacci series Repeat: ζ := Ui − Uj if ζ < 0. for instance with M = 714025. 1] X has expectation 0 and variance 1. Extending to random variables from other distributions X: = ∑ for Ui ∼ U[0. a = 1366. set i := 17 if j = 0.F.. set ζ := ζ + 1 Ui := ζ i := i − 1 j := j − 1 if i = 0.

b): The c. say x.d. of a random variable X. compute FX-1 (U) . The proof: P(X <= x) = P(FX-1 (U) <= x) = P(U <= FX (x)) = FX (x). obtain a value. FX (x).a)U ): To generate a random variable from the exponential distribution Exp( = To generate a random variable with p. say u. To generate a random variable from the uniform distribution U(a. and set it equal to x.f. So to get a value.X = FX-1 (U) has c.d.d.f. of a random variable U. .f. is X= = a + (b .

and Cauchy. Weibull. uniform. Unfortunately. logistic. for many probability distributions it is either impossible or extremely difficult to find the inverse transform. that is to solve .U = X2 X= Let Define Generate and . Distributions in this group are exponential. The distributions of In the particular case where X = U we have = and = To apply this method FX (x) must exist in a form for which the corresponding inverse transform can be found analytically. and are respectively: = U1/2 be IID random variable distributed and .

For instance. .U or U > p then X = 0. 1) 2) if U < 1 .. The Inverse Transform Method for simulating from continuous random variables have analog in the discrete case. To simulate X for which P(X = xj) = Pj let U be uniformly distributed over (0. Inverse Transform Method for simulating discrete random variables.f. P(X = 0) = 1 .p The algorithm: 1) generate U ~ U(0.p or p < 1 .with respect to x. P(X = xj) = Pj j=0. P(X = 1) = p. 1. otherwise X = 1. we see that X has the desired distribution.d. To Generate a Bernoulli random variable. if we want to simulate a random variable X having p.. 1). . and set As.

each of which is a success with probability p. Generate a random variable with the above distribution.U < (1 . Algorithm 1: 1) 2) 3) 4) generate U ~ U(0.1) if U<0. As since j-1 trials are all failures. random variable distributed .p)j or. Suppose we want to simulate X such that P(X = i) = p(1 .5 --> X=3 if U<0.p)j-1 < U < 1 .1) if U<0.5 --> X=2 if U<1 --> X=3 Algorithm 2: 1) 2) 3) 4) generate U ~ U(0.(1 .(1 . equivalently.p)i-1.p)j < 1 .Let be a. i >= 1 X can be thought of as representing the time of the first success when independent trials.2 --> X=1 if U<0.p)j-1 . are performed. for which (1 . We can simulate such a random variable by generating a random variable U and then setting X equal to that value j for which 1 .8 --> X=2 if U<1 --> X=1 To generate a geometric random variables.

then setting it follows that is a binomial random variable with parameters n and p. Go to 3.n We employ the inverse transform method by making use of the recursive identity With i denoting the value currently under consideration.1. That is. 2. 5. Generate a random number U. One difficulty with the above procedure is that is requires the generation of n random numbers. 3. F = F + pr.p)] To Simulate a binomial random variable. for which (1 . . equivalently. If U < F. Recall that . c = p/(1-p). A binomial (n. 4. and F = F(i) is the probability that X is less than or equal to i. pr = c(n – i)/(i + 1))pr. Un are independent uniform U(0. note first that the above procedure does not use the actual value of a random number U but only whether or not it exceeds p. pr = P(X = i) the probability that X is equal to i. .p)} = 1 + [log(U)/log(1 . set X = i and stop. . p) random variable can be most easily simulated as the sum of n independent Bernoulli random variables. if U1.or. i = 0. . i = i + 1.p) j < U} = min{j: j > log(U)/log(1 .…. i = 0. 1) variables. F = pr. . To show how to reduce the number of random numbers needed.p)j < U < (1 . the algorithm can be expressed as follows: 1.p)j-1 We can thus define X by X = min{j: (1 .

it follows that such a random variable can also be generated by subtracting from n the value of a binomial (n. the inverse transform algorithm for generating a Poisson random variable with the mean can be expressed as follows. . set X = i and stop. Whereas . we can generate a binomial (n. the above will need to make 1 + searches. 1 . If not. … The key to using the Inverse Transform Method to generate such a random variable is the following identity: . 3. and if so it The above algorithm successively checks whether the Poisson value is 0. Thus. p = p/(I + 1). is the probability that X is equal to i. p) random variable represents the number of successes in n independent trials when each is a success with probability p. 1 p). the number of searches it makes is 1 more than the value of X. it will take 1 + np searches to generate X. then whether it is 1.p) random variable by the above method and subtract its value from n to obtain the desired generation. The random variable X is Poisson with mean if i = 0. then it computes + by using the recursion. i = 0. Hence. Go to 3. Hence. on average. Since a binomial (n. 5. and so on. It now checks whether U < sets X = 1. then X = 1. If U < F. To see that the above algorithm does indeed generate a Poisson random variable with the mean . and so on.The preceding algorithm first check whether X = 0. (where the right-hand side is the new value of F). To Simulate a Poisson random variable. i = i + 1. 2. note that it first generates a random number U and then checks whether or not . then 2. when p > ½. F = p. Therefore. Generate a random number U. F = F + p. and F = F(i) is the probability that X is less than or equal to i) 1. i>=0 Upon using thr above recursion to compute the Poisson probabilities as they become needed. 1. itsets X = 0. the number of comparisons needed will be 1 greater than the generated value of the Poisson. 4. The quantity i refers to the value presently under consideration. If so. on average. and so on. Hence.

1) |) where X ~ N( . rather than starting at 0 and working upward. The algorithm lets I = int( ) and computes (by first taking logarithms and then by raising the result to the power e). since a Poisson random variable with the mean is most likely to take on one of the two integral values closest to . Indeed. It then searches downward starting from X = I in the case where X <= I and upward starting from X = I + 1 otherwise. It now generates a Poisson random variable with the mean by generating a random number U . The number of searches needing by the algorithm is roughly 1 more than the absolute difference between the random variable X and its mean . rather than where Z ~ N(0. Since.798 That is. it follows that average numbers of searches ~ 1 + E(|X = 1+ = 1+ = 1 + 0. It then uses the above recursion to determine F(I). ) . a more efficient algorithm would first check one of these values. the average numbers of searches grows with the square root of with as becomes larger and larger. for large a Poisson is (by the central limit theorem) approximately normal with mean and variance both equal to . and then noting whether or not X <= I by seeing whether or not U <= F(I).this is fine when is small. it can be greatly improved upon when is large.