Random number generators and random processes Eugeniy E. Mikhailov - - PowerPoint PPT Presentation

random number generators and random processes
SMART_READER_LITE
LIVE PREVIEW

Random number generators and random processes Eugeniy E. Mikhailov - - PowerPoint PPT Presentation

Random number generators and random processes Eugeniy E. Mikhailov The College of William & Mary Lecture 11 Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 1 / 11 Statistics and probability intro Mathematical and physical


slide-1
SLIDE 1

Random number generators and random processes

Eugeniy E. Mikhailov

The College of William & Mary

Lecture 11

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 1 / 11

slide-2
SLIDE 2

Statistics and probability intro

Mathematical and physical definition of probability (p) of event ’x’ px = lim

Ntotal→∞

Nx Ntotal where Nx the number of registered event ’x’ Ntotal the total number of all events

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 2 / 11

slide-3
SLIDE 3

Peg board example

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 3 / 11

slide-4
SLIDE 4

Peg board example

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 3 / 11

slide-5
SLIDE 5

Peg board example

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 3 / 11

slide-6
SLIDE 6

Peg board example

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 3 / 11

slide-7
SLIDE 7

Peg board example

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 3 / 11

slide-8
SLIDE 8

Peg board example

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 3 / 11

slide-9
SLIDE 9

Peg board example

Example of 104 balls runs over 40 layers of nails x=pegboard(10^4, 40); hist(x, [-25:25]);

200 400 600 800 1000 1200 1400

  • 20
  • 10

10 20

Resulting distribution is Gaussian

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 3 / 11

slide-10
SLIDE 10

Probability for occurrence of the real number

It is well known that interval [0..1] has infinite amount of reals numbers (as well as any other non zero interval). So there is very little or may be zero chance that event will repeat or even happen. Since we cannot run ∞ number of tests. In this case we should speak about probability density p(x). The best way to estimate it is from a histogram. Run you N tests with numbers distributed between 0 and 1. Split you interval at m bins and calculate number of events when you hit each bin h(xb). Plot this vs bin positions.

Easy to do with Matlab

r = rand(1, N); hist(r, m); Then p(x) = lim

N,m→∞

h(xb nearest to x) N

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 4 / 11

slide-11
SLIDE 11

Uniform random distribution

r = rand(1, 10000); hist(r, 10);

200 400 600 800 1000 1200 0.2 0.4 0.6 0.8 1

r = rand(1, 10000); hist(r, 100);

20 40 60 80 100 120 140 0.2 0.4 0.6 0.8 1

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 5 / 11

slide-12
SLIDE 12

Random number generators

How can a computer, which is very accurate, precise, and deterministic, generate random numbers? It cannot! Instead we can generate a sequence of pseudo random numbers. By pseudo we mean that starting from the same initial conditions the computer will generate the same sequence of numbers (very handy for debugging). But otherwise it will look like random numbers and will have statistical properties of random numbers.

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 6 / 11

slide-13
SLIDE 13

Linear Congruential Generator (LCG)

Recursive formula

ri+1 = (ari + c) mod m here m the modulus a multiplier, 0 < a < m c increment, c ≤ c < m r1 seed value, 0 ≤ r1 < m All pseudo random generators have a period and this one is no

  • exception. Note that once ri repeat one of the previews values the

sequence will restart. This one can have at most a period of m distinct numbers (0..m).

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 7 / 11

slide-14
SLIDE 14

Linear Congruential Generator (LCG) continued

Bad choice of a, c, m will lead to even shorter periods.

Example

m = 10, a = 2, c = 1 ,r1 = 1 r = [1, 3, 7, 5, 1] While the LCG has advantage of speed and simplicity. Do not use the LCG whenever your money or reputation are at stakes! While Matlab does not use LCG, many other programming languages use it as default in their libraries so be aware of it.

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 8 / 11

slide-15
SLIDE 15

Random number generators period

Even the best pseudo random generators cannot have a period larger than 2B, where B is number of memory bits. Can you prove it? While the period can be huge its not infinite. For example for Matlab R2007a the period is 219937 − 1. Why bother? Recall that for example error of the Monte Carlo integration method is ∼ 1/ √ N. This holds true only when N < than the period of random number generator (T). Otherwise the MC method cannot give uncertainty better than ∼ 1/ √

  • T. Further increase of the number of random points will not

bring any any extra improvement.

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 9 / 11

slide-16
SLIDE 16

How to check the random generator

Generally it is uneasy and probably impossible. However for us only statistical properties are of importance. So the easiest is to check that integral deviation calculated with Monte Carlo algorithm drops as 1/ √ N.

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 10 / 11

slide-17
SLIDE 17

Simple LCG check

Let’s see how the LCG will look with very bad coefficient m = 10, a = 2, c = 1 ,r1 = 1

10-4 10-3 10-2 10-1 100 101 102 103 104 105

Lines are fro MC with rand and line-crosses for lcgrand(Nrows, Ncols, 2,1,10); function r= ... lcgrand(Nrows,Ncols, ... a,c,m, seed) r=zeros(Nrows, Ncols); r(1)=seed; cntr=1; for i=2:Nrows*Ncols; r(i)= mod( (a*r(i-1)+c), m); end r=r/(m-1); %normalization end check_lcgrand

Eugeniy Mikhailov (W&M) Practical Computing Lecture 11 11 / 11