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:
According to Tutorialspoint
Last lesson: The function ldiv () in C
Next lesson: The function srand () in C
Similar Post
- The RAND function returns a random real number, greater than or equal to 0 and less than and equal to 1
- How to generate random numbers in Google Sheets
- How to create random text in Word
- Save time with these text formatting functions in Microsoft Excel
- Function in programming C
- DAY function in SQL Server