23
Random Numbers

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

Embed Size (px)

Citation preview

Random Numbers

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.

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

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

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.

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.

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.

Midsquare Method, example

x0 = 5497

x1: 54972 = 30217009 x1 = 2170, R1 = 0.2170

x2: 21702 = 04708900 x2 = 7089, R2 = 0.7089

x3: 70892 = 50253921 x3 = 2539, R3 = 0.2539

Drawback: Hard to state conditions for picking initial seed that will generate a “good” sequence.

Midsquare Generator, examples

“Bad” sequences:• x0 = 5197

x1: 51972 = 27008809 x1 = 0088, R1 = 0.0088 x2: 00882 = 00007744 x2 = 0077, R2 = 0.0077 x3: 00772 = 00005929 x3 = 0059, R3 = 0.0059

• xi = 6500xi+1: 65002=42250000 xi+1=2500, Ri+1= 0.2500xi+2: 25002=06250000 xi+2=2500, Ri+1= 0.2500

Linear Congruential Generator (LCG) Generator

Start with random seed Z0 < m = largest possible integer on machineRecursively generate integers between 0 and M Zi = (a Zi-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

Linear Congruential Generator (LCG) (Lehmer 1951)

Let Zi be the ith number (integer) in the sequence

Zi = (aZi-1+c)mod(m) Zi{0,1,2,…,m-1}

where Z0 = seed

a = multiplier

c = increment

m = modulus

Define Ui = Zi /m (to obtain U(0,1) value)

LCG, example

16-bit machine

a = 1217 c = 0 Z0 = 23 m = 215-1 = 32767

Z1 = (1217*23) mod 32767 = 27991

U1 = 27991/32767 = 0.85424

Z2 = (1217*27991) mod 32767 = 20134

U2 = 20134/32767 = 0.61446

An LCG can be expressed as a function of the seed Z0

THEOREM:

Zi = [aiZ0+c(ai-1)/(a-1)] mod(m)

Proof: By induction on i

i=0 Z0 = [a0Z0+c(a0-1)/(a-1)] mod(m)

Assume for i. Show that expression holds for i+1

Zi+1 = [aZi+c] mod(m)

= [a {[aiZ0+c(ai-1)/(a-1)] mod(m)} +c] mod(m)

= [ai+1Z0+ac(ai-1)/(a-1) +c] mod(m)

= [ai+1Z0+c(ai+1-1)/(a-1) ] mod(m)

Examples:

Zi = (69069Zi-1+1) mod 232 Ui = Zi /232

Zi = (65539Zi-1+76) mod 231 Ui = Zi/231

Zi = (630360016Zi-1) mod (231-1) Ui = Zi/231

Zi = 1313Zi-1 mod 259 Ui = Zi/259

What makes one LCG better than another?

Mixed congruential generator is full period if

1. m = 2B (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,…)

A full period (full cycle) LCG generates all m values before it cycles.

Consider Zi = (3Zi-1+2) mod(9) with Z0 =7

Then Z1 = 5 Z2 = 8 Z3 = 8 Zj = 8 j = 3,4,5,6,…

On the other hand Zi = (4Zi-1+2) mod(9) has full period.

Why?

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.

ExamplesZi+1 = (16807Zi+3) mod (451605),

where 16807 =75, 16806 =(2)(3)(2801), 451605 =(3)(5)(7)(11)(17)(23)

This LCG does not satisfy the first two conditions.

Zi+1 = (16807Zi+5) mod (635493681)where 16807 =75, 16806 = (2)(3)(2801), 635493681 = (34)(28012)

This LCG satisfies all three conditions.

- m = 2B 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 Zn = (aZn-1) mod(m) Zn = (anZ0) mod(m).

If m = 2B, where B = # bits in the machine, the longest period is m/4 (best one can do) if and only if

— Z0 is odd— a = 8k+ 3, kZ+ (5,11,13,19,21,27,…)

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 (2l-1)x2M-1 when m=2M

Add-with-carry & Subtract-with-borrow

Similar to LFG AWC: X

n=(X

n-l+X

n-k+carry) mod m

SWB: Xn=(X

n-l-X

n-k-carry) mod m

Multiply-with-carry Generators

Similar to LCG X

n=(aX

n-1+carry) mod m

Random Number Generation

Other kinds of generators• Quadratic Congruential Generator

– Snew = (a1 Sold2 + a2 Sold

2 + b) mod L

• Combination of Generators– Shuffling – L’Ecuyer – Wichman/Hill

• Tausworthe Generator– Generates sequence of random bits

Assignment

Generate 20 Pseudorandom number using:

1. Midsquare Method

2. Multiplicative Linear Congruential Generator (LCG) Generator

3. Mixed Linear Congruential Generator (LCG) Generator