# Difference between rand() and srand() function in C

### What is the rand()?

The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a game in C using some random number from 1 to 9.

In this case, we can use the rand() function to create a game. In the case of programs, when the rand() function generates without calling the srand() function, the program can produce the same value when it is running. rand() function generates by some algorithm.

This algorithm gives a series of numbers unrelated to each other when the rand() and function are calls.

Example- When we generate the random function between 1 to 9, then we write the function like the below instruction:

`Num = rand() % 9 + 1`

Syntax:

`int rand(Void);`

Parameters – The rand() function does not use any parameters.

Return value - The rand() function returns the pseudo number in RAND_MAX. The range of the return value is between 0 to RAND_MAX.

If we generate 10 random numbers by the C with the help of the rand() function in a loop, then whenever we compile and run the program, the output sequence is always the same. The program is created where the same sequence is in the random numbers.

Program –

``````#include <stdio.h>
#include <conio.h>
int main (void)
{
Printf (“generated random numbers are: “);
for (int i = 0; i < 3; i++)
printf (“%d”, rand());
return 0; } ``````

Output

When the first time we generate the code, the output will be –

`generated random numbers are: 123 567 456`

When the second time we generate the code, the output will be –

`generated random numbers are: 123 567 456`

When the nth time we generate the code, the output will be –

`generated random numbers are: 123 567 456`

This is the program using the rand() function. The above program is generated the same sequence of a random number in the output whenever the program run. So the output is the same in every program.

### What is srand()?

Strand() is another important function in C. srand() is the in-built function in C. srand() defined by the <stdlib.h> header file in the C program. The argument is passed like a seed.

In the program, if srand() is not called, then rand() seed set. If srand() = srand(1) then the program start. The seed value is used to srand() to generate the different pseudo functions. Any value of the seed is set the develop another starting point.

Syntax – int srand ( unsigned seed );

Parameters - The srand() function use parameter seed. The seed parameter uses to generate the pseudo-random integers.

Return value – The srand() function returns the pseudo-generated random number. This function should be seeded or reseeded. When anyone want to generate a new series of pseudo numbers.

Program –

``````#include <stdio.h>
#include <stdlib.h> #include <time.h>
int main (void)
{
Srand (time (0));
Printf (“generated random numbers are: “);
for (int i = 0; i < 3; i++)
printf (“%d”, srand());
return 0; }
``````

Output – When the first time we generate the code, the output will be –

`generated random numbers are: 123 567 456`

When the second time we generate the code, the output will be –

`generated random numbers are: 127 547 436`

When the nth time we generate the code, the output will be –

`generated random numbers are: 523 467 156`

This is the program using srand() function. The above program generates a different sequence of a random number in the output every time program runs.

So the main difference between the rand() and srand() function using C is the rand() function generates the same output every time of program execution; the srand() function does not generate the same result every time of program execution. The syntax, parameter all the values are different in the rand and srand.