Download CS61B Lecture #31: Pseudo-Random Numbers and Sequences and more Slides Data Structures and Algorithms in PDF only on Docsity!
CS61B Lecture
Today:
- Pseudo-random Numbers (Chapter 11)
- What use are random sequences?
- What are “random sequences”?
- Pseudo-random sequences.
- How to get one.
- Relevant Java library classes and methods.
- Random permutations.
Coming Up: Concurrency and synchronization ( Data Structures, Chap- ter 10, and Programming Into Java, Chapter 9).
Why Random Sequences?
- Choose statistical samples
- Simulations
- Random algorithms
- Cryptography:
- Choosing random keys
- Generating streams of random bits (e.g., SSL xor’s your data with a regeneratable, pseudo-random bit stream that only you and the recipient can generate).
- And, of course, games
Pseudo-Random Sequences
- Even if definable, a “truly” random sequence is difficult for a com- puter (or human) to produce.
- For most purposes, need only a sequence that satisfies certain sta- tistical properties, even if deterministic.
- Sometimes (e.g., cryptography) need sequence that is hard or im- practical to predict.
- Pseudo-random sequence: deterministic sequence that passes some given set of statistical tests.
- For example, look at lengths of runs: increasing or decreasing con- tiguous subsequences.
- Unfortunately, statistical criteria to be used are quite involved. For details, see Knuth.
Generating Pseudo-Random Sequences
- Not as easy as you might think.
- Seemingly complex jumbling methods can give rise to bad sequences.
- Linear congruential method is a simple method that has withstood test of time:
X 0 = arbitrary seed
Xi = (aXi− 1 + c) mod m, i > 0
- Usually, m is large power of 2.
- For best results, want a ≡ 5 mod 8, and a, c, m with no common factors.
- This gives generator with a period of m (length of sequence before repetition), and reasonable potency (measures certain dependencies
among adjacent Xi.)
- Also want bits of a to “have no obvious pattern” and pass certain other tests (see Knuth).
- Java uses a = 25214903917, c = 11, m = 2^48 , to compute 48-bit pseudo-random numbers but I haven’t checked to see how good this is.
Other Generators
Xn =
arbitary value, n < 55
(Xn− 24 + Xn− 55 ) mod 2e, n ≥ 55
- Other choices than 24 and 55 possible.
- This one has period of 2 f^ (2^55 − 1), for some f < e.
- Simple implementation with circular buffer:
i = (i+1) % 55; X[i] += X[(i+31) % 55]; // Why +31 (55-24) instead of -24? return X[i]; /* modulo 232 */
- where X[0 .. 54] is initialized to some “random” initial seed val- ues.
Adjusting Range and Distribution
- Given raw sequence of numbers, Xi, from above methods in range
(e.g.) 0 to 248 , how to get uniform random integers in range 0 to
n − 1?
- If n = 2k, is easy: use top k bits of next Xi (bottom k bits not as “random”)
- For other n, be careful of slight biases at the ends. For example, if
we compute Xi/(2^48 /n) using all integer division, and if (2^48 /n) doesn’t
come out even, then you can get n as a result (which you don’t want).
- Easy enough to fix with floating point, but can also do with integers;
one method (used by Java for type int):
/** Random integer in the range 0 .. n-1, n>0. */ int nextInt (int n) { long X = next random long ( 0 ≤ X < 248 ); if (n is 2 k for some k) return top k bits of X; int MAX = largest multiple of n that is < 248 ; while (Xi >= MAX) X = next random long ( 0 ≤ X < 248 ); return Xi / (MAX/n); }
Other Distributions
- Can also turn uniform random integers into arbitrary other distri- butions, like the Gaussian.
1 y
x
P (x)
- Curve is the desired probability distribution (P (x) is the probability
that a certain random variable is ≤ x.)
- Choose y uniformly between 0 and 1, and the corresponding x will be
distributed according to P.
Computing Arbitrary Discrete Distribution
- Example from book: want integer values Xi with Pr(Xi = 0) = 1/ 12 ,
Pr(Xi = 1) = 1/ 2 , Pr(Xi = 2) = 1/ 3 , Pr(Xi = 3) = 1/ 12 :
Legend: 0: 1: 2: 3:
- To get desired probabilities, choose floating-point number, 0 ≤ Ri <
4 , and see what color you land on.
- ≤ 2 colors in each beaker ≡ ≤ 2 colors between i and i + 1.
return (Ri % 1.0 > v[(int) Ri]) ? top[(int) Ri] : bot[Ri];
where v = { 1.0/3.0, 2.0/3.0, 0, 1.0/3.0 }; top = { 1, 2, 2, 1 }, bot = { 0, 1, /* ANY */ 0, 3 };
Shuffling
- A shuffle is a random permutation of some sequence.
- Obvious dumb technique for sorting N -element list:
- Generate N random numbers
- Attach each to one of the list elements
- Sort the list using random numbers as keys.
- Can do quite a bit better:
void shuffle (List L, Random R) for (int i = L.size (); i > 0; i -= 1) swap element i-1 of L with element R.nextInt (i) of L;
Swap items 0 1 2 3 4 5 Start A♣ 2 ♣ 3 ♣ A♥ 2 ♥ 3 ♥ 5 ⇐⇒ 1 A♣ 3 ♥ 3 ♣ A♥ 2 ♥ 2 ♣ 4 ⇐⇒ 2 A♣ 3 ♥ 2 ♥ A♥ 3 ♣ 2 ♣
Swap items 0 1 2 3 4 5 3 ⇐⇒ 3 A♣ 3 ♥ 2 ♥ A♥ 3 ♣ 2 ♣ 2 ⇐⇒ 0 2 ♥ 3 ♥ A♣A♥ 3 ♣ 2 ♣ 1 ⇐⇒ 0 3 ♥ 2 ♥ A♣A♥ 3 ♣ 2 ♣
Random Selection
- Same technique would allow us to select N items from list:
/** Permute L and return sublist of K>=0 randomly
- chosen elements of L, using R as random source. */ List select (List L, int k, Random R) { for (int i = L.size (); i+k > L.size (); i -= 1) swap element i-1 of L with element R.nextInt (i) of L; return L.sublist (L.size ()-k, L.size ()); }
- Not terribly efficient for selecting random sequence of K distinct
integers from [0..N ), with K N.