Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pseudo-random numbers

Similar presentations


Presentation on theme: "Pseudo-random numbers"— Presentation transcript:

1 Pseudo-random numbers

2 Outline In this lesson, we will: Introduce random numbers
Introduce psuedo-random numbers Describe how they are generated Describe the weaknesses Give some warnings

3 Random numbers A random number is any number that cannot be predicted
Flipping a fair coin or rolling a fair die is likely close to random Picking a card out of a deck is only random if the deck is appropriately shuffled Radioactive decay is one common source of random numbers Please visit For $57.12, you can purchase the Rand Corporation’s A Million Random Digits with 100,000 Normal Deviates Atmospheric noise is also a source Please visit

4 Random numbers A few points:
It is difficult for a computer to generate truly random numbers You often don’t want truly random numbers… …you want unpredictable pseudo-random numbers What you also need is repeatability of pseudo-random numbers Suppose you were able to produce a bug using random numbers How do you reproduce that bug???

5 Pseudo-random numbers
The function int std::rand() found in the library cstdlib produces a pseudo-random number between 0 and RAND_MAX, inclusive Ask it for n random numbers, restart the program, and it will give you the same random numbers each time On ecelinux with g++, RAND_MAX is (= 231 – 1), we start with:

6 Pseudo-random numbers
If you want an integer from m to n, inclusive, int r{ (std::rand() % (n - m + 1)) + m }; If you want a uniformly distributed floating-point number on [0, 1], double r{std::rand()/static_cast<double>(RAND_MAX)}; If you want a uniformly distributed floating-point number on (0, 1), double r{(std::rand() + 1.0)/(RAND_MAX + 2.0)}; If you want a floating-point number on (a, b) or [a, b], take appropriate previous results and apply the affine mapping (b – a)r + a

7 Pseudo-random numbers
How are these numbers generated? Arithmetic is used to generate a sequence of numbers that is unpredictable at first glance and thus appears to be random Here is a simple implementation1 using a global variable my_seed: unsigned long my_seed{0}; unsigned int my_rand() { my_seed = L*my_seed L; return my_seed; } void my_srand( unsigned long s ) { my_seed = s; 1 Press et al., “Numeric Recipes in C”, 2nd Ed.

8 Pseudo-random numbers
In your course on discrete mathematics and logic, you will see that number theory guarantees that this will produce a sequence of 264 different seed values and 264 are relatively prime They share no common factors This will therefore produce 264 different random integers before it begins to repeat itself If you give it a seed, it will only give you a different sub-sequence of that longer sequence of integers

9 Pseudo-random numbers
This algorithm falls into a class of algorithms described as linear congruential generators Good for simulations, horrible for cryptography, gambling, etc. If you know the seed, it is perfectly predictable Do not author your own random number generators: Use one designed by a mathematician Always remember, these are pseudo-random, not random: “Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin.” John von Neumann

10 Setting the seed You can change the seed using the
void srand( unsigned int s ); function If you start with the same seed, it will give you the same sequence of numbers If you want a more-or-less unreproducible random numbers, use the current time as a seed Number of seconds since January 1st, 1970 #include <cstdlib> #include <ctime> int main() { std::srand( std::time( nullptr ) ); // continue... }

11 Summary Following this lesson, you now
Understand random numbers and pseudo-random numbers Know about std::rand() and std::srand(...) Understand these numbers are produced using arithetic They are not random Understand that these are not secure Only good for simulations

12 References [1] Wikipedia [2] cplusplus.com [3] WH Press, SA Teukolsky, WT Vetterling and BP Flannery, “Numerical Recipies in C: The Art of Scientific Computing”, 2nd Ed., Cambridge University Press, 1992.

13 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

14 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a s_result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Pseudo-random numbers"

Similar presentations


Ads by Google