1 / 13

CS221

CS221. Random Numbers. Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers that come up on a real roulette wheel are random

hunter
Download Presentation

CS221

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS221 Random Numbers

  2. Random Numbers • Random numbers are often very important in programming • Suppose you are writing a program to play the game of roulette • The numbers that come up on a real roulette wheel are random • Writing a program with predictable values doesn't make for a very interesting roulette game • We want to simulate randomness in the computer! • What about computerized slot machines?

  3. Computers and Random Numbers • Computers are “deterministic” machines • Computers are not capable of true randomness • Instead, pseudo-random numbers are used • Pseudo-random numbers are generated using a mathematical formula • There is a pattern in pseudo-random numbers • The pattern is nearly impossible to observe

  4. Advantage of Psuedo-Randomness • It would be difficult to debug programs that use truly random numbers (can’t recreate error cases on demand) • By providing the same start-value (seed) to the generator, the same sequence results on each run and error cases can be observed • When you are through debugging, you can use a different seed each time. • Often, this is done by using the system clock

  5. Random #s in C++ • The <cstdlib> header file should be included • Functions provided: • void srand(unsigned int seed); • This function sets the seed for the random number generator to the value of the seed parameter • int rand(); • This function generates and returns an integer value in the range 0..RAND_MAX (which is a defined constant in cstdlib)

  6. Random #s in C++ • srand() is usually only called ONE time • rand() is called every time a random number is desired • If you want a number between 0 and N • val = rand() % (N+1) • If you want a number with a range M..N • val = rand() % (N-M+1) + M

  7. #include <cstdlib> //req'd for srand and rand #include <iostream> //req'd for cout and cin using namespace std; int main() { double avg = 0.0; int i, minX = 30, maxX = 50; int randVal, seed; cout << "Enter seed: "; cin >> seed; srand(seed); srand() is usually called only one time to start a sequence rand() is called each time a pseudo-random number is needed for (i = 0; i < 10; i++) { randVal = rand() % (maxX - minX + 1) + minX; cout << randVal << endl; } for (i = 0; i < 10000; i++) { avg += rand() % (maxX - minX + 1) + minX; } avg = avg / 10000; cout << "Avg of 10000: " << avg << endl; return (0); }

  8. Enter seed: 12 42 46 40 41 40 43 32 38 31 42 Avg of 10000: 39.9971 Enter seed: 1652 38 32 43 43 36 48 34 48 31 49 Avg of 10000: 40.0484 Enter seed: 12 42 46 40 41 40 43 32 38 31 42 Avg of 10000: 39.9971 Note: Same seed = same sequence = same results

  9. C++ Random # Generator • The C++ random number generator DOES produce a repeating sequence of pseudo-random numbers • The "period" of the generator is so large, though, that it is not usually a problem • The "period" of a random number generator is how large the pattern that repeats is • C++'s random number generator has a period of 232 • rand() will re-start the same repeating sequence after generating about 4.3 billion random numbers • This is large enough for most applications requiring random number generation

  10. //include iostream, cstdlib, and cmath int main() { long int stopX; srand(100); stopX = (long int)pow(2, 32) - 1; long int li; cout << "RANDOM #" << rand() << endl; cout << "RANDOM #" << rand() << endl; for (li = 0; li < stopX; li++) rand(); cout << "RANDOM #" << rand() << endl; cout << "RANDOM #" << rand() << endl; for (li = 0; li < stopX; li++) rand(); cout << "RANDOM #" << rand() << endl; cout << "RANDOM #" << rand() << endl; return (0); } RANDOM #12662 RANDOM #23392 RANDOM #12662 RANDOM #23392 RANDOM #12662 RANDOM #23392 While the pattern DOES repeat, there are about 4.3 billion "random" numbers generated in the pattern. (This program took about 29 minutes of time to run)

  11. Using Time to Seed the Generator • If you hard code the seed or allow the user to enter a seed, simulated randomness is lost • Most programmers use the “time” function to seed the generator • This lessens the probability that a user can determine the pattern of psuedo-random numbers

  12. Using Time to Seed the Generator • Must include the <time.h> library • Provides function “time( )” • Calling function “time( )” • time( NULL ) • Returns the current time from the system clock • Expressed in number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC

  13. #include <cstdlib> //req'd for srand and rand #include <iostream> //req'd for cout and cin #include <time.h> //req’d for time using namespace std; int main() { double avg = 0.0; int i, minX = 30, maxX = 50; int randVal, seed; srand( time(NULL) ); srand() is usually called only one time to start a sequence Using the clock to seed the generator instead of user input rand() is called each time a pseudo-random number is needed for (i = 0; i < 10; i++) { randVal = rand() % (maxX - minX + 1) + minX; cout << randVal << endl; } for (i = 0; i < 10000; i++) { avg += rand() % (maxX - minX + 1) + minX; } avg = avg / 10000; cout << "Avg of 10000: " << avg << endl; return (0); }

More Related