Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rand() and srand() in C++

Tags:

c++

random

What is the basis of generating random numbers in C++?

Is there some logic or principle behind that?

Are the numbers generated completely random?

Suppose I am running this program:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    /*
    Declare variable to hold seconds on clock.
    */
    time_t seconds;
    /*
    Get value from system clock and
    place in seconds variable.
    */
    time(&seconds);
    /*
    Convert seconds to a unsigned
    integer.
    */
    srand((unsigned int) seconds);
    /*
    Output random values.
    */
    cout<< rand() << endl;
    cout<< rand() << endl;
    cout<< rand() << endl;
    return 0;
}

What it shows: http://img14.imageshack.us/img14/1538/98271820.png

It showed 205 twice.

like image 317
Manoj Avatar asked Mar 03 '13 16:03

Manoj


2 Answers

The question was basically answered in comments and another answer, but I'll gather it up in one place.

C++ rand() function produces not a truly random sequence of numbers, but a pseudo-random one. This means that it is basically a pre-defined sequence of numbers which are "random", but fixed somewhere (actually, it's more complex than that, but this is a simplification for better understanding). Think of it as a long list of integers.

Each call to rand() function pulls the current number and moves the pointer to "current "random" number" to the next one.

What srand() function does is basically setting the pointer to some location in the list. If you don't call the srand() function on each launch, or call it with fixed parameter (seed), you will have the same sequence of numbers on each program launch.

When you're setting your seed from the seconds, if you launch your program twice within that second, your seed will be the same - hence producing the same result.

Try the following code:

#include <windows.h>
// << other code >>
for (int i=0; i<50; i++) {
    time(&seconds);
    srand(seconds);
    cout<< seconds<<" "<<rand()<<endl;
    Sleep(100);
}

You will notice, that each "seconds" value correspond to some fixed "first" value for the rand() function.

like image 111
DarkWanderer Avatar answered Sep 30 '22 05:09

DarkWanderer


Starting with the second question:

Are the numbers generated completely random?

No, that is very unlikely to ever happen in a computer. They are "pseudo-random" numbers, which is some sequence of numbers that vary in range over time in a random-like fashion. But if you start with the same "seed", you get the same sequence each time. This predictability is sometimes very useful, as it allows repeating the same experiment several times with the same outcome - altering the seed, will allow a similar run to have a different outcome.

The function srand sets the seed. Some systems do have a function called randomize, but it is not part of the standard as such. If it does exist it sets the seed to something unknown to the code - such as the current time in milliseconds.

Is there some logic or principle behind that?

Yes. There are several methods for generating pseudo-randum numbers. Simple ones can be written in one or two lines of C code using regular int or long types, and just consists of taking the "current value" + some constant, multiplied by some large number and modulo some other large number.

More complex ones involve dozens of more lines of rather complicated math with large numbers - for example Mersenne Twister is a recent work that is available as source code if you search a little bit.

like image 31
Mats Petersson Avatar answered Sep 30 '22 07:09

Mats Petersson