Pseudo-random numbers

Slides:



Advertisements
Similar presentations
Main memory.
Advertisements

Recursive binary search
For loops.
Templates.
Binary and hexadecimal numbers
Static variables.
Default values of parameters
The structured programming theorem
Pointers.
Switch statement.
Do-while loops.
Command-line arguments
Pointer arithmetic.
Console input.
Dangling pointers.
Hello world!.
This.
Arithmetic operators.
Sorted arrays.
Floating-point primitive data types
Dynamically allocating arrays within structures
Break statements.
Wild pointers.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
The call stack and recursion and parameters revisited
The ternary conditional operator
Dynamically allocating structures
Bit-wise and bit-shift operators
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Repetitious operations
Dynamically allocating arrays
Insertion sort.
Problems with pointers
Protecting pointers.
Code-development strategies
Throwing exceptions.
Console output.
Selection sort.
Insertion sort.
Pointers as arguments and return values
Addresses and pointers
Default values of parameters
Pointer arithmetic.
Recursive functions.
Class variables and class functions
Operator overloading.
The std::string class.
Templates.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
Arithmetic operators.
Constructors.
This.
Recursive binary search
The structured programming theorem
Recursive functions.
Algorithms and templates
Presentation transcript:

Pseudo-random numbers

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

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 https://qrng.anu.edu.au/ 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 https://www.random.org/

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???

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 2147483647 (= 231 – 1), we start with: 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421

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

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 = 1664525L*my_seed + 1013904223L; return my_seed; } void my_srand( unsigned long s ) { my_seed = s; 1013904223 1196435762 3519870697 2868466484 1649599747 2670642822 1476291629 2748932008 2180890343 2498801434 3421909937 3167820124 2636375307 3801544430 28987765 2210837584 3039689583 1 Press et al., “Numeric Recipes in C”, 2nd Ed.

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 1664525 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

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

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... }

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

References [1] Wikipedia https://en.wikipedia.org/wiki/Linear_congruential_generator [2] cplusplus.com http://www.cplusplus.com/reference/cstdlib/rand/ http://www.cplusplus.com/reference/cstdlib/srand/ [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.

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 https://www.rbg.ca/ for more information.

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.