The function srand () in C

The function void srand (unsigned int seed) provides seed for the random number generator used by the rand function.

The function void srand (unsigned int seed) provides seed for the random number generator used by the rand function.

Declaring the function srand () in C

Below is the declaration for srand () in C:

 void srand ( unsigned int seed ) 

Parameters

seed : is an integer value, used as a seed by a random number algorithm.

Returns the value

This function does not return any values.

For example

The following C program illustrates the usage of srand () in C:

 #include #include #include int main () { int i , n ; time_t t ; n = 5 ; /* Khoi tao bo sinh so ngau nhien */ srand (( unsigned ) time (& t )); /* in 5 so ngau nhien trong day tu 0 toi 50 */ for ( i = 0 ; i < n ; i ++ ) { printf ( "%dn" , rand () % 50 ); } return ( 0 ); } 

Compiling and running the above C program will result:

The function srand () in C Picture 1The function srand () in C Picture 1

According to Tutorialspoint

Previous article: Function rand () in C

Next lesson: Function mblen () in C

5 ★ | 1 Vote