CS221

13
CS221 Random Numbers

description

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 - PowerPoint PPT Presentation

Transcript of CS221

Page 1: CS221

CS221

Random Numbers

Page 2: CS221

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?

Page 3: CS221

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

Page 4: CS221

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

Page 5: CS221

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)

Page 6: CS221

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

Page 7: CS221

#include <cstdlib> //req'd for srand and rand#include <iostream> //req'd for cout and cinusing namespace std;

int main(){ double avg = 0.0; int i, minX = 30, maxX = 50; int randVal, seed; cout << "Enter seed: "; cin >> seed; srand(seed); 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);}

srand() is usually called onlyone time to start a sequence

rand() is called each time apseudo-random numberis needed

Page 8: CS221

Enter seed: 1242464041404332383142Avg of 10000: 39.9971

Enter seed: 165238324343364834483149Avg of 10000: 40.0484

Enter seed: 1242464041404332383142Avg of 10000: 39.9971

Note: Same seed = same sequence = same results

Page 9: CS221

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

Page 10: CS221

//include iostream, cstdlib, and cmathint 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 #12662RANDOM #23392RANDOM #12662RANDOM #23392RANDOM #12662RANDOM #23392

While the pattern DOESrepeat, there are about 4.3 billion "random"numbers generatedin the pattern.(This program took about29 minutes of time to run)

Page 11: CS221

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

Page 12: CS221

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

Page 13: CS221

#include <cstdlib> //req'd for srand and rand#include <iostream> //req'd for cout and cin#include <time.h> //req’d for timeusing namespace std;

int main(){ double avg = 0.0; int i, minX = 30, maxX = 50; int randVal, seed; srand( time(NULL) );

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);}

srand() is usually called onlyone time to start a sequence

Using the clock to seed thegenerator instead of user input

rand() is called each time apseudo-random numberis needed