# Random by yaofenji

VIEWS: 13 PAGES: 13

• pg 1
```									Random Numbers

Andrew Morgan
University of Michigan
1
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 the roulette wheel
are random
 Writing a program such that the numbers that
come up the same every time doesn't make for a
very interesting roulette game
 What   about computerized slot machines?
2
Computers and Random Numbers
 Computers are “deterministic” machines
 Computers can not generate truly
"random" numbers
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

3
 It is difficult to debug programs that use
truly random numbers
 By providing the same start-value (seed)
to the generator, the same sequence
results
 When you are through debugging, you can
use a different seed each time.
 Often,
this is done by using a portion of the
system clock
4
Random #s in C++
 The <cstdlib> header file should be
includedd
 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)

5
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

6
#include <cstdlib> //req'd for srand and rand
#include <iostream> //req'd for cout and cin
using namespace std;                               srand() is usually called only
one time to start a sequence
int main()
{
double avg = 0.0;                                rand() is called each time a
int    i, minX = 30, maxX = 50;                  pseudo-random number
int    randVal, seed;                            is needed
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);
}

7
Enter seed: 12          Enter seed: 1652        Enter seed: 12
42                      38                      42
46                      32                      46
40                      43                      40
41                      43                      41
40                      36                      40
43                      48                      43
32                      34                      32
38                      48                      38
31                      31                      31
42                      49                      42
Avg of 10000: 39.9971   Avg of 10000: 40.0484   Avg of 10000: 39.9971

Note: Same seed = same sequence = same results
8
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
32
 C++'s random number generator has a period of 2
 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
9
//include iostream, cstdlib, and cmath
int main()                                      RANDOM #12662
{                                               RANDOM #23392
long int stopX;
srand(100);
RANDOM #12662
stopX = (long int)pow(2, 32) - 1;             RANDOM #23392
long int li;                                  RANDOM #12662
RANDOM #23392
cout << "RANDOM   #" << rand() << endl;
cout << "RANDOM   #" << rand() << endl;
for (li = 0; li   < stopX; li++)
rand();                                 While the pattern DOES
cout << "RANDOM   #" << rand() << endl;   repeat, there are about
cout << "RANDOM   #" << rand() << endl;   4.3 billion "random"
for (li = 0; li   < stopX; li++)
rand();
numbers generated
cout << "RANDOM   #" << rand() << endl;   in the pattern.
cout << "RANDOM   #" << rand() << endl;   (This program took about
return (0);                               29 minutes of time to run)
}

10
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                                   11
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

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

13

```
To top