11/21/19 Notes by Jae random number generation (RNG) Generating random numbers 3 Randomization + pseudo random number generation (PRNG) Using random numbers Birthday Paradox ; ~25 people in room, prob > < that 2 or more people will share the same birthday Sources 1. 2. 3. 4. 5. 6. ask human system clock least significant digits last four digits for phone, SSN random.org “at morphemic noise” (interferences) books listing random numbers transcendental numbers Old Sources • flip coin • roll dice Applications • encryption • video gaming • lottery tickets/cosmos UUID = Universally Unique Identifier GUID = Globally Unique Identifier Linear congruential generator multiplier: π₯@A; = (ππ₯@ + π) πππ π Random permutation: 1, 2, 3, … , π for i in range (n) get random number between i and n-1 r = randInt(0, n-1-i) + i swap(array, i, r) Las Vegas Algorithms Randomized algorithms that always get the correct answer time is random 1. 2. 3. bogosort: finds random permutation and checks if sorted quicksort/quickselect randomized pivot rabin’s closest point Monte Carb Algorithms Time is constrained, but the “answer” given is not always correct Ex. Estimate π x = Math.random( ) y = Math.random( ) 1 some points will fall in circle (p) some points will fall outside circle (100 – p) 1 < π Y2Z π΄OPQORS πππππ‘π ππ π‘βπ ππππππ = = ⇒π= π΄TUVWQS 1 π‘ππ‘ππ ππ’ππππ ππ πππππ‘π π 1 100 Y Z 2 < = π 25 isPrime? Prime = divisible only by 1 and itself 1. 2. 3. 4. divide π vy every number 2 up to π − 1 if any divide evenly, then composite otherwise it is prime stop searching at √π sieve of Eratosthenes filter out by all multiples of 2, 3, 5, … , √π, what’s left is prime Fermar’s Little Theorem (randomized approach) (π@k; ≡ 1) πππ π ⇒ (π@ ≡ π) πππ π ππ π ππ πππππ ∀ πππ 1 < π < π Select π values of π (π; , π< , πp , … , π@ ) and see if πP@k; ≡ 1 πππ π? If any πP fails (πP@k; ! = 1) then composite (not prime) If none fail, then probably prime Random Binary Search π(log π) Harmonic Numbers Series π(log π) Karger’s Min-Cut Algorithm 11/25/19 Notes by Jae Approximation Algorithms Approach to solving “intractable” — cannot be solved deterministically in polynomial time Approximation Scheme Way of asserting how close the approximation will be to the optimal solution 1. Absolute error bound |πΆ ∗ (π₯) − πΆ(π₯)| ≤ π (π₯) ← functional of input size (more tolerant of larger error for larger problem size) ↑ ↑ ↑ Optimal 2. 3. Computed error by approx. alg Ratio error bound πΆ ∗ (π₯) πΆ(π₯) max | , } ≤ π(π₯) πΆ(π₯) πΆ ∗ (π₯) for minimization problem, πΆ ∗ (π₯) ≤ πΆ(π₯) maximization problem, πΆ ∗ (π₯) ≥ πΆ(π₯) Relative error bound