Presentation is loading. Please wait.

Presentation is loading. Please wait.

Random Numbers. Two Types of Random Numbers 1.True random numbers: True random numbers are generated in non- deterministic ways. They are not predictable.

Similar presentations


Presentation on theme: "Random Numbers. Two Types of Random Numbers 1.True random numbers: True random numbers are generated in non- deterministic ways. They are not predictable."— Presentation transcript:

1 Random Numbers

2 Two Types of Random Numbers 1.True random numbers: True random numbers are generated in non- deterministic ways. They are not predictable. They are not repeatable. 2.Pseudorandom numbers: Pseudorandom numbers are numbers that appear random, but are obtained in a deterministic, repeatable, and predictable manner.

3 True Random Numbers Generators  Use one of several sources of randomness –decay times of radioactive material –electrical noise from a resistor or semiconductor –radio channel or audible noise –keyboard timings  some are better than others  usually slower than PRNGs

4 Pseudorandom Numbers Generators Reasons for pseudorandom numbers: –Flexible policies –Lack of knowledge Generate stochastic processes Decision making (random decision) Numerical analysis (numerical integration) Monte Carlo integration

5 Pseudorandom Numbers Generators The desirable Properties of Pseudorandom Numbers –Uncorrelated Sequences - The sequences of random numbers should be serially uncorrelated –Long Period - The generator should be of long period (ideally, the generator should not repeat; practically, the repetition should occur only after the generation of a very large set of random numbers). –Uniformity - The sequence of random numbers should be uniform, and unbiased. That is, equal fractions of random numbers should fall into equal ``areas'' in space. Eg. if random numbers on [0,1) are to be generated, it would be poor practice were more than half to fall into [0, 0.1), presuming the sample size is sufficiently large. –Efficiency - The generator should be efficient. Low overhead for massively parallel computations.

6 How to generate pseudorandom random numbers Random number seed: Virtually all computer methods of random number generation start with an initial random number seed. This seed is used to generate the next random number and then is transformed into a new seed value.

7 Midsquare Method 1.Start with an initial seed (e.g. a 4-digit integer). 2.Square the number. 3.Take the middle 4 digits. 4.This value becomes the new seed. Divide the number by 10,000. This becomes the random number. Go to 2.

8 Midsquare Method, example x 0 = 5497 x 1 : 5497 2 = 30217009  x 1 = 2170, R 1 = 0.2170 x 2 : 2170 2 = 04708900  x 2 = 7089, R 2 = 0.7089 x 3 : 7089 2 = 50253921  x 3 = 2539, R 3 = 0.2539 Drawback: Hard to state conditions for picking initial seed that will generate a “good” sequence.

9 Midsquare Generator, examples “Bad” sequences: x 0 = 5197 x 1 : 5197 2 = 27008809  x 1 = 0088, R 1 = 0.0088 x 2 : 0088 2 = 00007744  x 2 = 0077, R 2 = 0.0077 x 3 : 0077 2 = 00005929  x 3 = 0059, R 3 = 0.0059 x i = 6500 x i+1 : 6500 2 =42250000  x i+1 =2500, R i+1 = 0.2500 x i+2 : 2500 2 =06250000  x i+2 =2500, R i+1 = 0.2500

10 Linear Congruential Generator (LCG) Generator Start with random seed Z 0 < m = largest possible integer on machine Recursively generate integers between 0 and M Z i = (a Z i-1 + c) mod m Use U = Z/m for pseudo-random number get (avoid 0 and 1) When c = 0  Called Multiplicative Congruential Generator When c > 0  Mixed LCG

11 Linear Congruential Generator (LCG) (Lehmer 1951) Let Z i be the i th number (integer) in the sequence Z i = (aZ i-1 +c)mod(m) Z i  {0,1,2,…,m-1} where Z 0 = seed a = multiplier c = increment m = modulus DefineU i = Z i /m (to obtain U(0,1) value)

12 LCG, example 16-bit machine a = 1217c = 0 Z 0 = 23m = 2 15 -1 = 32767 Z 1 = (1217*23) mod 32767 = 27991 U 1 = 27991/32767 = 0.85424 Z 2 = (1217*27991) mod 32767 = 20134 U 2 = 20134/32767 = 0.61446

13 An LCG can be expressed as a function of the seed Z 0 THEOREM: Z i = [a i Z 0 +c(a i -1)/(a-1)] mod(m) Proof:By induction on i i=0Z 0 = [a 0 Z 0 +c(a 0 -1)/(a-1)] mod(m) Assume for i. Show that expression holds for i+1 Z i+1 = [aZ i +c] mod(m) = [a {[a i Z 0 +c(a i -1)/(a-1)] mod(m)} +c] mod(m) = [a i+1 Z 0 +ac(a i -1)/(a-1) +c] mod(m) = [a i+1 Z 0 +c(a i+1 -1)/(a-1) ] mod(m)

14 Examples: Z i = (69069Z i-1 +1) mod 2 32 U i = Z i /2 32 Z i = (65539Z i-1 +76) mod 2 31 U i = Z i /2 31 Z i = (630360016Z i-1 ) mod (2 31 -1)U i = Z i /2 31 Z i = 13 13 Z i-1 mod 2 59 U i = Z i /2 59 What makes one LCG better than another?

15 Mixed congruential generator is full period if 1.m = 2 B (B is often # bits in word) fast 2.c and m relatively prime (g.c.d. = 1) 3.If 4 divides m, then 4 divides a – 1 (e.g., a = 1, 5, 9, 13,…)

16 A full period (full cycle) LCG generates all m values before it cycles. Consider Z i = (3Z i-1 +2) mod(9) with Z 0 =7 Then Z 1 = 5 Z 2 = 8 Z 3 = 8 Z j = 8 j = 3,4,5,6,… On the other hand Z i = (4Z i-1 +2) mod(9) has full period. Why?

17 The period of an LCG is m (full period or full cycle) if and only if —If q is a prime that divides m, then q divides a-1 —The only positive integer that divides both m and c is 1 —If 4 divides m, then 4 divides a-1. Examples Z i+1 = (16807Z i +3) mod (451605), where 16807 =7 5, 16806 =(2)(3)(2801), 451605 =(3)(5)(7)(11)(17)(23) This LCG does not satisfy the first two conditions. Z i+1 = (16807Z i +5) mod (635493681) where 16807 =7 5, 16806 = (2)(3)(2801), 635493681 = (3 4 )(2801 2 ) This LCG satisfies all three conditions.

18 - m = 2 B where B = # bits in the machine is often a good choice to maximize the period. - If c = 0, we have a power residue or multiplicative generator. Note that Z n = (aZ n-1 ) mod(m)  Z n = (a n Z 0 ) mod(m). If m = 2 B, where B = # bits in the machine, the longest period is m/4 (best one can do) if and only if —Z 0 is odd —a = 8k+ 3, k  Z + (5,11,13,19,21,27,…)

19 Lagged Fibonacci Generators  Similar to Fibonacci Sequence  Increasingly popular  X n = (X n-l + X n-k ) mod m (l>k>0)  l seeds are needed  m usually a power of 2  Maximum period of (2 l -1)x2 M-1 when m=2 M

20 Add-with-carry & Subtract-with-borrow  Similar to LFG  AWC: X n =(X n-l +X n-k +carry) mod m  SWB: X n =(X n-l -X n-k -carry) mod m

21 Multiply-with-carry Generators  Similar to LCG  X n =(aX n-1 +carry) mod m

22 Random Number Generation Other kinds of generators Quadratic Congruential Generator –S new = (a 1 S old 2 + a 2 S old 2 + b) mod L Combination of Generators –Shuffling – L’Ecuyer – Wichman/Hill Tausworthe Generator –Generates sequence of random bits

23 Assignment Generate 20 Pseudorandom number using: 1.Midsquare Method 2.Multiplicative Linear Congruential Generator (LCG) Generator 3.Mixed Linear Congruential Generator (LCG) Generator


Download ppt "Random Numbers. Two Types of Random Numbers 1.True random numbers: True random numbers are generated in non- deterministic ways. They are not predictable."

Similar presentations


Ads by Google