What is the difference between rand and srand?


I would like if you could explain to me what the difference is, I am very confused with rand and srand, what is the difference? I've searched elsewhere, but I confuse more. Thanks.

asked by Sofía Videa 22.07.2018 в 02:52

3 answers


As you have probably read the rand () function every time we call it, it returns a pseudo-random integer between 0 and the RAND_MAX (this constant is defined in the library). But there is a disagreement, every time you call this function within the program, it will generate a sequence, for example:

int main ()
   rand();//Supongamos que genera un 3
   rand();//Supongamos que genera un 12
   rand();//Supongamos que genera un 7
   return 0;

But if you run the program again, it will generate that same sequence. Part of an initial number (called a seed), get some accounts and get a random number. For the second number, put some accounts with the previous result and get a second number and so on.

To avoid this, the srand function is used, so that the seed is different, and the resulting sequence is different each time the program is executed. Normally, for this seed to be different, the date, time of the system is used, using the time () function, or the number of the program process that changes each time the program is started is used. get with the getpid () function. The srand function is only necessary to call only once at the beginning of the program.

Importar las librerias
int main ()
   srand(time(NULL));//puedes usar esta
   srand(getpid());//o esta, pero llamas la función una vez
//En esta ocasión te dará secuencias diferentes cada vez que se ejecute el programa
   return 0;

Now, as an additional data, if at any time you want to change the scale of the number resulting from rand () you can go to the calculation of the module, for example:

v1 = rand() % 100;         // v1 esta en el rango 0 to 99
v2 = rand() % 100 + 1;     // v2 esta en el rango 1 to 100
v3 = rand() % 30 + 1985;   // v3 esta en el rango 1985-2014

I hope I have resolved your doubt.

answered by 22.07.2018 / 04:08

You could answer your question by reading the documentation of the functions that cause you doubts:

Unfortunately, the documentation is in English and you may not be fluent in the language. Luckily, cppreference has automatic translations of content ( std::rand in Spanish std::srand in Spanish ) and more luck yet there is StackOverflow in Spanish!.


Returns an integer value between 0 and RAND_MAX (both included) distributing the values in a homogeneous way, this means that any number within that range has the same probabilities of being chosen (1 probability among RAND_MAX ) .

If std::rand() is called before any call std::srand() will behave as if the seed had set 1 . Each time a seed is assigned to srd::rand() with std::srand() , it must produce the same sequence of values in successive calls. Other functions in the standard library might call std::rand() .


Set the seed that std::rand() will use to generate pseudo-random numbers.

Do not use std::rand .

The std::rand() function belongs to the C libraries, since you have labeled the question as C ++, you should not use C utilities because they may not be portable and may offer questionable results and performance. Specifically, is studying deprecar std::rand() .

Starting with the C ++ 11 standard, the C ++ language offers a complete library of generation of pseudorandom numbers that allows to choose the probability distribution (uniform, Bernoulli, Poisson, normal, discrete, constant, linear ...), the underlying type of the generated value and even the algorithm to be used (minstd, mt19937, ranlux, knuth ...).

answered by 23.07.2018 в 10:05

Well, I will try to summarize the concept as best I can:

srand : used to start the random number generator, creating a "seed".

rand : simply give you a number.

Try the following:

a) generate random numbers by starting the "seed" (srand) with a fixed value, and you will see that with each execution of your code you will get the same random list. Ideal to repeat the same experiment. That is, each seed will generate a different list of random (called pseudo-random) and the same seed will always return the same list of random values (pseudo-random).

b) start srand with the current system time (usually the value used as seed). This will give you a random generation different from the execution of your code.

answered by 22.07.2018 в 03:58