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
5 ★ | 1 Vote
You should read it
- Call the function by pointer in C ++
- Function realloc () in C
- Malloc () function in C
- Function abs () in C
- The function getenv () in C
- The abort () function in C
- Pass cursor to function in C ++
- Div () function in C
- Cursor this in C ++
- Labs () function in C
- Random function in Excel (RAND function), how to use the Random function and examples
- The function ldiv () in C
Maybe you are interested
How to Build a Random Word Generator Using Bash in Linux
What is brand management? How to manage a brand well
Meaningful life lessons in the movie Wicked by Ariana Grande, Cynthia Erivo
How to generate random numbers in Excel
Is cheap, unbranded RAM really good?
4 warning signs to stay away from a smart home brand