Thursday Week 9
Random Numbers | 1/15 |
Random numbers are useful
... Random Numbers | 2/15 |
But how can a computer pick a number at random?
Software can only produce pseudo random numbers.
A Random Number Generator | 3/15 |
The most widely-used technique is called the Linear Congruential Generator (LCG)
... A Random Number Generator | 4/15 |
LCG is not good for applications that need extremely high-quality random numbers
... A Random Number Generator | 5/15 |
Trivial example:
11, 28, 29, 9, 6, 4, 13, 19, 23, 5, 24, 16, 21, 14, 30, 20, 3, 2, 22, 25, 27, 18, 12, 8, 26, 7, 15, 10, 17, 1, 11, 28, 29, 9, 6, 4, 13, 19, 23, 5, 24, 16, 21, 14, 30, 20, 3, 2, 22, 25, 27, 18, 12, 8, 26, 7, 15, 10, 17, 1, 11, 28, 29, 9, 6, 4, 13, 19, 23, 5, 24, 16, 21, 14, 30, 20, 3, 2, 22, 25, 27, 18, 12, 8, 26, 7, 15, 10, 17, 1, 11, 28, 29, 9, 6, 4, 13, 19, 23, 5, 24, 16, 21, 14, 30, 20, 3, 2, 22, 25, 27, 18, 12, 8, 26, 7, 15, 10, 17, 1, 11, 28, 29, 9, 6, 4, 13, 19, 23, 5, 24, 16, 21, 14, 30, 20, 3, 2, 22, 25, 27, 18, 12, 8, 26, 7, 15, 10, 17, 1, ...
... A Random Number Generator | 6/15 |
Another trivial example:
12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6, 12, 24, 18, 6, ...
... A Random Number Generator | 7/15 |
It is a complex task to pick good numbers. A bit of history:
Lewis, Goodman and Miller (1969) suggested
gcc
... A Random Number Generator | 8/15 |
man random
man srandom
srandom(int seed)// sets its argument as the seed
random()// uses a LCG technique to generate random // numbers in the range 0 .. RAND_MAX
where the constant RAND_MAX
stdlib.h
(depends on the computer: on the CSE network, RAND_MAX = 2147483647)
Exercise: Random Numbers | 9/15 |
Write a program randolf.c
srandom()
random()
... Exercise: Random Numbers | 10/15 |
To convert to a number between 0 .. RANGE
Seeding | 11/15 |
There is one significant problem with randolf.c
time(NULL)
time
time(NULL)// returns the time as the number of seconds // since the Epoch, 1970-01-01 00:00:00 +0000 // time(NULL) on October 1st, 2015, 12:59pm was 1443668340 // time(NULL) about a minute later was 1443668402
Exercise: Seeding | 12/15 |
Modify randolf.c
time()
time.h
Exercise: Coin Tossing | 13/15 |
Write a program that
Measure its performance using the UNIX time
... Exercise: Coin Tossing | 14/15 |
What you can observe:
Tips for Next Week's Lab | 15/15 |
BSTs, Files, Random Numbers
EOF
EOF
'a'..'z'
Produced: 5 Oct 2016