Randomnumbers
Randomnumbers
Pseudorandom Numbers
Michael Goodrich
CS 165
https://fitforrandomness.files.wordpress.com/2010/11/dilbert-does-randomness.jpg
https://xkcd.com/221/ Page2
Random number sequence definitions
Randomness of a sequence is the
Kolmogorov complexity of the
sequence (size of smallest Turing
machine that generates the
sequence) – infinite sequence should
require infinite size Turing machine.
Page3
Random number sequence definitions
Each element is chosen independently
from a probability distribution
[Donald Knuth].
Page4
Environmental Sources of Randomness
Radioactive decay http://www.fourmilab.ch/hotbits/
Page5
Combining Sources of Randomness
Suppose r1, r2, …, rk are random numbers from
different sources. E.g.,
b = r1 Å r2 Å … Å rk
Page6
Skew Correction
Von Neumann’s algorithm – converts biased random
bits to unbiased random bits:
Efficiency?
John von Neumann
Page7
Chi Square Test
Experiment with k outcomes, performed n times.
p1, …, pk denote probability of each outcome
Y1, …, Yk denote number of times each outcome occured
Page8
Analysis of random.org numbers
John Walker’s Ent program
Page9
Analysis of JPEG file
Entropy = 7.980627 bits per character.
Optimum compression would reduce the size of this
51768 character file by 0 percent.
Chi square distribution for 51768 samples is
1542.26, and randomly would exceed this value
0.01 percent of the times.
Arithmetic mean value of data bytes is 125.93
(127.5 = random).
Monte Carlo value for Pi is 3.169834647 (error
0.90 percent).
Serial correlation coefficient is 0.004249
(totally uncorrelated = 0.0).
Page10
Pseudorandom Number Generators
• A pseudorandom number generator (PRNG) is an
algorithm for generating a sequence of numbers
whose properties approximate the properties of
sequences of random numbers.
• The PRNG-generated sequence is not truly
random, because it is completely determined by an
initial value, called the PRNG's seed (which may
include truly random values).
• Although sequences that are closer to truly
random can be generated using hardware random
number generators, pseudorandom number
generators are important in practice for their
speed and reproducibility.
Page11
Pseudorandom Number Generators
• PRNGs are central in applications such as
simulations (e.g. for the Monte Carlo method),
electronic games (e.g. for procedural generation),
and cryptography.
• Cryptographic applications require the output not
to be predictable from earlier outputs.
Page13
Linear Congruential Generator (LCG)
x0 = given, x n+1 = P1 xn + P2 (mod N) n = 0,1,2,... (*)
Sequence: 79, 48, 95, 56, 99, 8, 75, 96, 68, 36, 39, 28, 35, 76, 59, 88,
15, 16, 79, 48, 95
ANSI C rand():
Page15
Plot (xi, xi+1)
Page16
Plot (xi, xi+1)
Page17
(xi, xi+1), (xi,xi+2), (xi, xi+2)
http://www.math.utah.edu/~alfeld/Random/Random.html
Page18
Visual Test in 3D
• Three-dimensional plot of 100,000 values
generated with IBM RANDU routine. Each point
represents 3 consecutive pseudorandom values.
• It is clearly seen that the points fall in 15 two-
dimensional planes.
Page19
Matsumoto’s Marsenne Twister
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
Page20
Example Visual Test
Page21
Cryptographically Strong Pseudorandom
Number Generator
Page22
Hash/Encryption Chains
Last bit
of xi+1
key Hash or Encryption Function xi+1
xi
Page23
Some Cryptographic Hash Functions
• SHA-1 Hash function https://en.wikipedia.org/wiki/SHA-1
Page24
BBS “secure” random bits
BBS (Blum, Blum and Shub, 1984)
– Based on difficulty of factoring, or finding
square roots modulo n = pq.
Fixed For a particular bit seq.
• p and q are primes such • Seed: random x
that p = q = 3 (mod 4) relatively prime to n.
• n = pq (is called a Blum • Initial state: x0 = x2
integer) • ith state: xi = (xi-1)2
• ith bit: lsb of xi
Note that: x 0 =-2 i mod f ( n )
xi (mod n)
Therefore knowing p and q allows us to find x0 from xi
25
Random Numbers in Python
https://docs.python.org/3/library/random.html
26