# rand() and srand() in C/C++

• Difficulty Level : Easy
• Last Updated : 21 Jun, 2022

rand ()

The rand() function is used in C/C++ to generate random numbers in the range [0, RAND_MAX). Note: If random numbers are generated with rand() without first calling srand(), your program will create the same sequence of numbers each time it runs.

Syntax:

``` int rand(void):
returns a pseudo-random number in the range of [0, RAND_MAX).
RAND_MAX: is a constant whose default value may vary
\between implementations but it is granted to be at least 32767.```

Say if we are generating 5 random numbers in C with the help of rand() in a loop, then every time we compile and run the program our output must be the same sequence of numbers.

## C++

 `// C++ program to demonstrate the use of` `// rand()`   `#include ` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``// This program will create same sequence of ` `    ``// random numbers on every program run `   `    ``for``(``int` `i = 0; i < 5; i++)` `        ``cout << ``rand``() << ``" "``;` `  `  `    ``return` `0;` `}`   `// This code is contributed by sarajadhav12052009`

## C

 `#include ` `#include `   `int` `main(``void``)` `{` `    ``// This program will create same sequence of ` `    ``// random numbers on every program run `   `    ``for``(``int` `i = 0; i<5; i++)` `        ``printf``(``" %d "``, ``rand``());` `  `  `    ``return` `0;` `}`

NOTE: This program will create same sequence of random numbers on every program run.
Output 1:

`453 1276 3425 89`

Output 2:

`453 1276 3425 89`

Output n:

`453 1276 3425 89`

srand()

The srand() function sets the starting point for producing a series of pseudo-random integers. If srand() is not called, the rand() seed is set as if srand(1) were called at program start. Any other value for seed sets the generator to a different starting point.

Syntax:

```void srand( unsigned seed ):
Seeds the pseudo-random number generator used by rand() with the value seed.```

Note: The pseudo-random number generator should only be seeded once, before any calls to rand(), and the start of the program. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.

Standard practice is to use the result of a call to srand(time(0)) as the seed. However, time() returns a time_t value which vary everytime and hence the pseudo-random number vary for every program call.

## C++

 `// C++ program to generate random numbers`   `#include ` `#include ` `#include` `using` `namespace` `std;`   `int` `main()` `{` `    ``// This program will create different sequence of ` `    ``// random numbers on every program run `   `    ``// Use current time as seed for random generator` `    ``srand``(``time``(0));`   `    ``for``(``int` `i = 0; i < 4; i++)` `        ``cout << ``rand``() << ``" "``;`   `    ``return` `0;` `}`   `// This code is contributed by sarajadhav12052009`

## C

 `// C program to generate random numbers` `#include ` `#include ` `#include`   `// Driver program` `int` `main(``void``)` `{` `    ``// This program will create different sequence of ` `    ``// random numbers on every program run `   `    ``// Use current time as seed for random generator` `    ``srand``(``time``(0));`   `    ``for``(``int` `i = 0; i<4; i++)` `        ``printf``(``" %d "``, ``rand``());`   `    ``return` `0;` `}`

NOTE: This program will create different sequence of random numbers on every program run.
Output 1:

`453 1432 325 89`

Output 2:

`8976 21234 45 8975`

Output n:

`563 9873 12321 24132`

How srand() and rand() are related to each other?

srand() sets the seed which is used by rand to generate “random” numbers. If you don’t call srand before your first call to rand, it’s as if you had called srand(1) to set the seed to one.
In short, srand() — Set Seed for rand() Function