Unit09 1 PRNG
Unit09 1 PRNG
Randomness in Computation:
Random Number Generators
1
Last Unit
• Computer organization: what’s under the hood
3
This Unit
• Random number generation
4
Overview
• The concept of randomness
o Let’s wake up with a video about randomness
https://scratch.mit.edu/projects/25843319/
5
Let’s try in Excel and Python
• Generate random numbers in Excel
445 331 316 388 252 254 382 447 421 500
6
Randomness in Computing
• Determinism: input predictable output
7
What is Randomness?
Tricky philosophical and mathematical question
8
Defining Randomness
Philosophical question
9
Some Randomness
Properties
• A random sequence should not be biased: as length increases
no element should be any more frequent than others
an unfair coin is biased (Long sequences will have more heads):
H T T H H T H H T T H T H H H T H …
10
Random sequence should be
• Unbiased (no “loaded dice”)
14
Randomness is slippery!
• In summary, “random” to mean something like this:
no gambling strategy is possible
that allows a winner in the long run.
15
Randomness in computing
Why do we want it? How do we get it?
16
Why Randomness in Computing?
• Internet gambling and state lotteries
• Simulation
o (weather, evolution, finance [oops!], physical & biological sciences, …)
• Monte Carlo methods and randomized algorithms
o (evaluating integrals, …)
• Cryptography
o (secure Internet commerce, BitCoin, secret communications, …)
• Games, graphics, and many more
17
True Random Sequences
• Precomputed random sequences.
o For example, A Million Random Digits with 100,000 Normal Deviates
(1955): A 400 page reference book by the RAND corporation
• 2500 random digits on each page
• Generated from random electronic pulses
• True Random Number Generators (TRNG)
o Extract randomness from physical phenomena
such as atmospheric noise, times for radioactive decay
• Drawbacks:
o Physical process might be biased
(produce some values more frequently)
o Expensive
o Slow
18
Pseudorandom Sequences
• Pseudorandom number generator (PRNG):
algorithm that produces a sequence that looks random
(i.e. passes some randomness tests)
19
(Pseudo) Random Number Generator
• A (software) machine to produce sequence x1, x2, x3, x4, x5,…
from x0 x0
x2 PRNG
20
(Pseudo) Random Number Generator
• A (software) machine to produce sequence x1, x2, x3, x4, x5,…
from x0 x0
x0
• Get pseudorandom numbers: x1 PRNG
22
Linear Congruential Generator (LCG)
# global internal state/ seed
currentX = 0
prng_seed(6) 1 8 3 10 5 0 7 2 9 4 11 6
for i in range(12): 1 8 3 10 5 0 7 2
print(prng(), end=" ")
1 8 3 10 5 0 7 2 9 4 11 6
24
Example LCG
• First 20 numbers:
5, 0, 7, 2, 9, 4, 11, 6, 1, 8, 3, 10,
5, 0, 7, 2, 9, 4, 11, 6, ?
Random-looking?
• What do you think the next number in the sequence is?
• Moral: just eyeballing the sequence not a good test of
randomness!
• This generator has a period that is too short: it repeats too
soon.
(What else do you notice if you look at it for a while?)
25
Another PRNG
def prng2(n):
return (n + 8) % 12 # a=1, c=8, m=12
>>> prng_seed(6)
>>> for i in range(20):
print(prng(), end=" ")
[8, 4, 0, 8, 4, 0, 8, 4, 0, 8, 4, 0]
Random-looking?
8, 4, 0, 8, 4, 0, 8, 4, 0, 8, …
prng2, period = 3 next number = (last number + 8) mod 12
We want the longest period we can get!
27
Picking the constants a, c, m
• Large value for m, and appropriate values for a and c
that work with this m
a very long sequence before numbers
begin to repeat.
• Maximum period is m
28
Picking the constants a, c, m
• The LCG will have a period of m (the maximum) if
and only if:
o c and m are relatively prime (i.e. the only positive integer
that divides both c and m is 1)
o a-1 is divisible by all prime factors of m
o if m is a multiple of 4 , then a-1 is also a multiple of 4
• (Number theory tells us so)
30
Exercise for you
(1) c and m (3) if m is a multiple
relatively prime of 4, so is a-1
(2) a-1 divisible by all
prime factors of m
• Hidden correlations
33
Finding hidden correlations
Looking
good!
34
Finding hidden correlations
OOPS!
35
Advice from an expert
Get expert advice
36
Using random number
generators in Python
37
Random integers in Python
• To generate random integers in Python, we can use the
randint function from the random module.
• randint(a,b) returns an integer n such that
a ≤ n ≤ b (note that it’s inclusive)
>>> from random import randint
>>> randint(0,15110)
12838
>>> randint(0,15110)
5920
>>> randint(0,15110)
12723
41
List Comprehensions
One output from a random number generator not so
interesting when we are trying to see how it behaves
>>> randint(0, 99)
42
Easily get a list of outputs
>>> [ randint(0,99) for i in range(10) ]
[5, 94, 28, 95, 34, 49, 27, 28, 65, 65]
44
Adjusting Range
• Suppose we have a LCG with period n (n is very large)
45
Adjusting Range
• Specifically: our LCG is the Linear Congruential Generator of
glib (period = 231 = 2147483648)
• We call prng() and get numbers like
1533190675, 605224016, 450231881, 1443738446, …
• We define:
def roll_die():
roll = prng() % 6 + 1
assert 1 <= roll and roll <= 6
return roll
47
Random range
• Remember, prng() gives numbers like
1533190675, 605224016, 450231881, 1443738446, …
def election_year() :
year = ?
assert 1788 <= year and year <= 2012 and year % 4 == 0
return year
48
Random range
• First: think how many numbers are there in the range we want?
That is, how many elections from 1788 to 2012?
o 2012 – 1788? No!
o (2012 – 1788) / 4? Not quite! (there’s one extra)
o (2012 – 1788) / 4 + 1 = 57 elections
def election_year() :
election_number = prng() % ((2012 - 1788) // 4 + 1)
assert 0 <= election_number and election_number <= 56
year = election_number * 4 + 1788
assert 1788 <= year and year <= 2012 and year % 4 == 0
return year
The same reasoning will work for a random sampling of any arithmetic series.
(Just think of the series and let the random number generator take care of the randomness!)
o How many different numbers in the series?
If there are k, randomly generate a number from 0 to k.
o Are the numbers separated by a constant (like the 4 years between elections)?
If so, multiply by that constant.
o What’s the smallest number in the series?
Add it to the number you just generated.
52