# C rand()

The rand() function is defined in the stdlib.h header file. This function is used for Pseudo Random Number Generator(PRNG) within the range 0 to RAND_MAX. The RAND_MAX is a symbolic constant, depending on the C libraries its value greater but not less than 32767.

``````
int rand(void);
```
```

## rand() Parameters:

The rand() function does not take any parameter.

## rand() Return Value

The rand() function return value is an integer value between 0 and RAND_MAX.

Input Return Value
success integer value(0- RAND_MAX)

## Examples of rand()

### Example 1: Working of rand() function in C?

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

int main()
{
int k, num;
time_t tm;

num = 5;

/* Intializes random number*/
srand((unsigned) time(&tm;));

/* random numbers from 0 to 49 */
for( k = 0 ; k < num ; k++ ) {
printf("%d\n", rand() % 50);
}

return(0);
}
```
```

Output:

```
38
45
29
29
47
```

### Example 2: How rand() works in error case?

``````
#include <stdio.h>
#include <stdlib.h>
int main (){
printf (" Random number is: %d", rand());
printf ("\nRandom number is: %d", rand());

printf (" \n Random number is: %d", rand());
printf ("\n Random number is: %d", rand());
getch();
}
```
```

Output:

```
Random number is: 41
Random number is: 18467
Random number is: 6334
Random number is: 26500
```
VIEW ALL
VIEW ALL