16
Random Numbers CSE 331 Section 2 James Daly

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

Embed Size (px)

Citation preview

Page 1: 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

Random Numbers

CSE 331Section 2James Daly

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

Random.org

• Website that generates random numbers• Uses atmospheric noise picked up with a

radio• Limited quota

Page 6: 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

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

Page 7: 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

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

Page 8: 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

Linear Congruential Generator

• One of the oldest PRNGs• Defined by a recurrence relation

• Fast and simple• Requires very little memory

Page 9: 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

Linear Congruential Generator

• 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

Page 10: 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

RANDU

• LCG from the 1960s• (integer)• (floating point)

• Really bad (don’t use it!)• Many results from the 1970s are suspect

because of it

Page 11: 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

Lehmer RNG

• Another LCG with c = 0• Recommended constants

• m = 231-1 (a Mersenne prime)• a = 48271

Page 12: 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

Other LCGs: rand48

• Equivalent implementation for glibc and Java

• Uses m = 248, a = 25214903917, c = 11• Only returns the 32 high order bits instead

of all 48 bits• Very long period

Page 13: 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

Mersenne Twister

• Default PRNG for Python, Ruby, and Matlab• Uses a matrix recurrence• Advantages

• Very long period (219937 – 1)• Good distribution

• Disadvantages• Fairly slow• Very large state space• Description is outside the scope of this class

Page 14: 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

Primality Testing

• Some algorithms require large prime numbers• Cryptography• Hash tables

• Need to check number is prime• Obvious method: divide by every number up to • Requires roughly 2b divisions• Impractical for large numbers

Page 15: 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

Fermat’s Lesser Theorem

• If p is prime and 0 < a < p, then ap-1 = 1 (mod p)

• If an-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

Page 16: 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

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:250

• Roughly one in one quadrillion chance• More likely to have a hardware error than false

claim

• Very conservative except for the Carmichael numbers