Upload
patience-lewis
View
216
Download
4
Embed Size (px)
Citation preview
Random Numbers
CSE 331Section 2James Daly
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
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
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
Random.org
• Website that generates random numbers• Uses atmospheric noise picked up with a
radio• Limited quota
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
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
Linear Congruential Generator
• One of the oldest PRNGs• Defined by a recurrence relation
• Fast and simple• Requires very little memory
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
RANDU
• LCG from the 1960s• (integer)• (floating point)
• Really bad (don’t use it!)• Many results from the 1970s are suspect
because of it
Lehmer RNG
• Another LCG with c = 0• Recommended constants
• m = 231-1 (a Mersenne prime)• a = 48271
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
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
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
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
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