C Library Functions -srand()

C Standard Library - <stdlib.h>

Description

C Library Functionsvoid srand(unsigned int seed)Seeding by functionrandThe random number generator used.

Declaration

Below is the declaration of the srand() function.

void srand(unsigned int seed)

Parameter

  • seed-- This is an integer value used to seed the pseudo-random number generation algorithm.

Return Value

This function does not return any value.

Example

The following example demonstrates the usage of the srand() 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 50*/ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50); } return(0); }

Let us compile and run the above program, this will produce the following result:

38
45
29
29
47

C Standard Library - <stdlib.h>

other extensions