Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function srand in C++

Tags:

c++

random

This code

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

int main ()
{
    printf ("First number: %d\n", rand() % 100);
    srand ( time(NULL) );
    printf ("Random number: %d\n", rand() % 100);
    srand ( 1 );
    printf ("Again the first number: %d\n", rand() %100);

    return 0;
}

has the following output:

First number: 41
Random number: 13
Again the first number: 41

There is also the following rule:

Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

I understand the words but I just don't understand the method itself. Why did it return 41 again? Is it random or must it return the same result in every case according to this code?

like image 895
dato datuashvili Avatar asked Jul 21 '10 07:07

dato datuashvili


People also ask

What is function of Srand in C?

srand() uses its argument seed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is not called, the rand() seed is set as if srand(1) was called at program start. Any other value for seed sets the generator to a different starting point.

What is the difference between Rand and Srand in C?

The rand() function in C++ is used to generate random numbers; it will generate the same number every time we run the program. In order to seed the rand() function, srand(unsigned int seed) is used. The srand() function sets the initial point for generating the pseudo-random numbers.

What does Srand time 0 )) do in C?

time(0) gives the time in seconds since the Unix epoch, which is a pretty good "unpredictable" seed (you're guaranteed your seed will be the same only once, unless you start your program multiple times within the same second).

What library is Srand in?

The srand() function in C++ seeds the pseudo-random number generator used by the rand() function. It is defined in the cstdlib header file.


1 Answers

If you call rand() without first calling srand(), the behavior is as if you called srand() with 1 as the argument.

This behavior is defined in the original C standard. I don't have a full copy of it handy, but The Open Group's POSIX standards are the next best thing, as they incorporate the full C standard (with some extensions):

http://www.opengroup.org/onlinepubs/000095399/functions/rand.html

The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

The actual result of a rand() call for any given seed is implementation-defined, except that for any seed x, call n to rand() after the seeding of the generator must return the same result. So while on your implementation you will always get 41 on the first rand() call after calling srand(1), and you will always get the same result (whatever it is) for the second rand() call, other implementations may use a different algorithm that produces different results.

like image 116
Nicholas Knight Avatar answered Sep 19 '22 01:09

Nicholas Knight