Presentation is loading. Please wait.

Presentation is loading. Please wait.

Random Numbers CSE 331 Section 2 James Daly. Randomness Most algorithms we’ve talked about have been deterministic The same inputs always give the same.

Similar presentations


Presentation on theme: "Random Numbers CSE 331 Section 2 James Daly. Randomness Most algorithms we’ve talked about have been deterministic The same inputs always give the same."— Presentation transcript:

1 Random Numbers CSE 331 Section 2 James Daly

2 Randomness Most algorithms we’ve talked about have been deterministic The same inputs always give the same result Sometimes we want different results Many games Cards for poker, blackjack, etc Number for roulette Dice for craps, Monopoly, Settlers of Catan Pick an item arbitrarily Pivot for quicksort

3 Random Numbers Truly random numbers – use physical phenomenon Dice / Coins Atmospheric noise Radioactive decay Pseudo-random numbers – sequence generated from a key (seed) Not actually random

4 True Randomness Many measurable phenomenon happen randomly A radioactive atom may or may not decay The least significant bit of the clock when the user presses a key Takes a long time to fill up Blocks or stalls if you need a lot of it /dev/random on Unix-based systems

5 Random.org Website that generates random numbers Uses atmospheric noise picked up with a radio Limited quota

6 Pseudorandom numbers Generates a sequence on numbers according to an algorithm Starts with a seed value Often based on the system clock Be careful creating several in a row Using the same seed generates the same sequence Allows for a simulation to be repeated Ex: Get the same series of pieces in Tetris

7 Potential problems Given enough time, the sequence will repeat The period may be shorter than expected on certain inputs Some values may be correlated Capable of predicting upcoming numbers Numbers may not be uniformly distributed Certain numbers show up more often

8 Linear Congruential Generator

9 Very sensitive to the choices of constants c and m must be relatively prime a – 1 must be divisible by all prime factors of m a – 1 must be a multiple of 4 if m is too Otherwise it will not use the full period Could still have other problems

10 RANDU

11 Lehmer RNG Another LCG with c = 0 Recommended constants m = 2 31 -1 (a Mersenne prime) a = 48271

12 Other LCGs: rand48 Equivalent implementation for glibc and Java Uses m = 2 48, a = 25214903917, c = 11 Only returns the 32 high order bits instead of all 48 bits Very long period

13 Mersenne Twister Default PRNG for Python, Ruby, and Matlab Uses a matrix recurrence Advantages Very long period (2 19937 – 1) Good distribution Disadvantages Fairly slow Very large state space Description is outside the scope of this class

14 Primality Testing

15 Fermat’s Lesser Theorem If p is prime and 0 < a < p, then a p-1 = 1 (mod p) If a n-1 ≠ 1 (mod n) then n must be composite We call a a Fermat witness to the compositeness of n Otherwise n is probably prime We call a a Fermat liar if n is actually composite

16 Testing In general, at least half of all a are witnesses to n’s compositeness If we try k trials, the probability of getting a false positive is at most (1/2) k If we do 50 trials, probability is at most 1:2 50 Roughly one in one quadrillion chance More likely to have a hardware error than false claim Very conservative except for the Carmichael numbers


Download ppt "Random Numbers CSE 331 Section 2 James Daly. Randomness Most algorithms we’ve talked about have been deterministic The same inputs always give the same."

Similar presentations


Ads by Google