The function rand () in C

The function int rand (void) returns a random number in the range from 0 to RAND_MAX.

The function int rand (void) returns a random number in the range from 0 to RAND_MAX.

RAND_MAX is a constant with a variety of default values ​​(depending on a number of factors such as Operating System, .), but at least 32767.

Declare the function rand () in C

Below is the declaration for rand () in C:

 int  rand ( void ) 

Parameters

The rand function does not take any parameters

Returns the value

This function returns a random number in the range from 0 to RAND_MAX

For example

The following C program illustrates the usage of rand () 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 49 */ for (  i  = 0 ;  i  <  n  ;  i ++ ) {  printf ( "%dn" ,  rand () % 50 ); } return ( 0 ); } 

Compiling and running the above C program will result:

Picture 1 of The function rand () in C

According to Tutorialspoint

Last lesson: The function ldiv () in C

Next lesson: The function srand () in C

« PREV POST
READ NEXT »