C library functions -rand()

C Standard Library - <stdlib.h>

Description

C Library Functionsint rand(void)Returns a pseudo-random number in the rangeRAND_MAXbetween 0 and.

RAND_MAX is a constant whose default value may differ across different implementations, but the value is at least 32767.

Declaration

Below is the declaration of the rand() function.

int rand(void)

Parameter

  • NA

Return Value

This function returns an integer value ranging from 0 to RAND_MAX.

Example

The following example demonstrates the usage of the rand() function.

Example

#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int i, n; time_t t; n = 5; /*Initialize random number generator*/ srand((unsigned) time(&t)); /*Output 5 random numbers between 0 and 49*/ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50); } return(0); }

Let us compile and run the above program, which will produce the following results:

38
45
29
29
47

C Standard Library - <stdlib.h>

other extensions