3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

16
3. Random Number 3. Random Number Generator Generator

Transcript of 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Page 1: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

3 Random Number 3 Random Number GeneratorGenerator

3 Random Number 3 Random Number GeneratorGenerator

The Roulette and Dice

Mechanical random number generators

What is a Random Number

bull Follow a definite distribution usually uniform distribution

bull Uncorrelatedbull Unpredictable

3 70 1

4

Pseudo-Random Numbers

bull Truly random numbers can not be generated on a computer

bull Pseudo-random numbers follow a well-defined algorithm thus predictable and repeatable

bull Have nearly all the properties of true random numbers

Linear Congruential Generator (LCG)

bull One of the earliest and also fastest algorithm

xn+1 = (a xn + c ) mod mwhere 0 le xn lt m m is the modulus a is multiplier c is increment All of them are integers Choice of a c m must be done with special care

Choice of ParametersName m a (multiplier) c perio

d

ANSI C [rand()]

231 1103515245 12345 231

Park-MillerNR ran0()

231-1

16807 0 231-2

drand48() 248 25214903917 11 248

Hayes 64-bit

264 6364136223846793005

1 264

(a x + c) mod m

Short-Coming of LCG

xn

xn+1

When (xnxn+1) pairs are plotted for all n a lattice structure is shown

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 2: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

The Roulette and Dice

Mechanical random number generators

What is a Random Number

bull Follow a definite distribution usually uniform distribution

bull Uncorrelatedbull Unpredictable

3 70 1

4

Pseudo-Random Numbers

bull Truly random numbers can not be generated on a computer

bull Pseudo-random numbers follow a well-defined algorithm thus predictable and repeatable

bull Have nearly all the properties of true random numbers

Linear Congruential Generator (LCG)

bull One of the earliest and also fastest algorithm

xn+1 = (a xn + c ) mod mwhere 0 le xn lt m m is the modulus a is multiplier c is increment All of them are integers Choice of a c m must be done with special care

Choice of ParametersName m a (multiplier) c perio

d

ANSI C [rand()]

231 1103515245 12345 231

Park-MillerNR ran0()

231-1

16807 0 231-2

drand48() 248 25214903917 11 248

Hayes 64-bit

264 6364136223846793005

1 264

(a x + c) mod m

Short-Coming of LCG

xn

xn+1

When (xnxn+1) pairs are plotted for all n a lattice structure is shown

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 3: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

What is a Random Number

bull Follow a definite distribution usually uniform distribution

bull Uncorrelatedbull Unpredictable

3 70 1

4

Pseudo-Random Numbers

bull Truly random numbers can not be generated on a computer

bull Pseudo-random numbers follow a well-defined algorithm thus predictable and repeatable

bull Have nearly all the properties of true random numbers

Linear Congruential Generator (LCG)

bull One of the earliest and also fastest algorithm

xn+1 = (a xn + c ) mod mwhere 0 le xn lt m m is the modulus a is multiplier c is increment All of them are integers Choice of a c m must be done with special care

Choice of ParametersName m a (multiplier) c perio

d

ANSI C [rand()]

231 1103515245 12345 231

Park-MillerNR ran0()

231-1

16807 0 231-2

drand48() 248 25214903917 11 248

Hayes 64-bit

264 6364136223846793005

1 264

(a x + c) mod m

Short-Coming of LCG

xn

xn+1

When (xnxn+1) pairs are plotted for all n a lattice structure is shown

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 4: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Pseudo-Random Numbers

bull Truly random numbers can not be generated on a computer

bull Pseudo-random numbers follow a well-defined algorithm thus predictable and repeatable

bull Have nearly all the properties of true random numbers

Linear Congruential Generator (LCG)

bull One of the earliest and also fastest algorithm

xn+1 = (a xn + c ) mod mwhere 0 le xn lt m m is the modulus a is multiplier c is increment All of them are integers Choice of a c m must be done with special care

Choice of ParametersName m a (multiplier) c perio

d

ANSI C [rand()]

231 1103515245 12345 231

Park-MillerNR ran0()

231-1

16807 0 231-2

drand48() 248 25214903917 11 248

Hayes 64-bit

264 6364136223846793005

1 264

(a x + c) mod m

Short-Coming of LCG

xn

xn+1

When (xnxn+1) pairs are plotted for all n a lattice structure is shown

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 5: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Linear Congruential Generator (LCG)

bull One of the earliest and also fastest algorithm

xn+1 = (a xn + c ) mod mwhere 0 le xn lt m m is the modulus a is multiplier c is increment All of them are integers Choice of a c m must be done with special care

Choice of ParametersName m a (multiplier) c perio

d

ANSI C [rand()]

231 1103515245 12345 231

Park-MillerNR ran0()

231-1

16807 0 231-2

drand48() 248 25214903917 11 248

Hayes 64-bit

264 6364136223846793005

1 264

(a x + c) mod m

Short-Coming of LCG

xn

xn+1

When (xnxn+1) pairs are plotted for all n a lattice structure is shown

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 6: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Choice of ParametersName m a (multiplier) c perio

d

ANSI C [rand()]

231 1103515245 12345 231

Park-MillerNR ran0()

231-1

16807 0 231-2

drand48() 248 25214903917 11 248

Hayes 64-bit

264 6364136223846793005

1 264

(a x + c) mod m

Short-Coming of LCG

xn

xn+1

When (xnxn+1) pairs are plotted for all n a lattice structure is shown

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 7: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Short-Coming of LCG

xn

xn+1

When (xnxn+1) pairs are plotted for all n a lattice structure is shown

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 8: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Other Modern Generators

bull Mersenne TwisterExtremely long period (219937-1) fast

bull Inversive Congruential Generatorxn = a xn+1 + c mod m

where m is a prime numberNonlinear no lattice structure

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 9: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Pick an Integer at Random

bull Suppose we want to select an integer j from 0 to N-1 with equal probability This can be done withj = Nwhere 0 lt 1 is uniformly

distributed random numberFor 2D lattice we can also do this if we

name the lattice site sequentially

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 10: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Pick j with Probability Pj

bull Since j Pj=1 we pick out j if is in the corresponding interval

0 1P0 P1

P=p[0]

j = 0

= drand64()

while(gtP) ++j P+=p[j]

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 11: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Pei Luchengrsquos Methodbull Use j = N to get an index pick a

final result based on the relative height This is an O(1) algorithm

1 2 3 4 5

12

4

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 12: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Non-Uniformly Distributed Random Numbers

bull Let F(x) be the cumulative distribution function of a random variable x then x can be generated fromx = F-1(ξ)where ξ is uniformly distributed between 0 and 1 and F-1(x) is the inverse function of F(x)

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 13: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Proof of the Inverse Method

bull The Mapping from x to ξ is one-to-one

bull The probability for ξ between value ξ and dξ is 1dξ which is the same as the probability for x between value x and dx Thus dξ = dF(x) = Frsquo(x)dx = p(x)dx

since F-1 (ξ)=x or ξ = F(x)

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 14: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Example 1 Exponential Distribution

bull P(x) = exp(-x) x ge 0 then

bull So we generate x byx = -log(ξ)where ξ is a uniformly distributed random number

0

F( ) exp( )d 1 exp( )x

x y y x

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 15: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Example 2 Gaussian distribution

bull Take 2D Gaussian distribution

bull Work in polar coordinates

2 21p( )d d exp ( ) 2 d d

2( )x y x y x y x y

21p( ) d d exp( 2) d d

2r r r r r r

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers

Page 16: 3. Random Number Generator. The Roulette and Dice Mechanical random number generators.

Box-Muller Methodbull The formula implies that the

variable θ is distributed uniformly between 0 and 2π frac12r2 is exponentially distributed we have

1 2

1 2

2log( ) cos(2 )

2log( ) sin(2 )

x

y

ξ1 and ξ2 are two independent uniformly distributed random numbers