The rand() function is defined in the stdlib.h header file. This function is used for Pseudo Random Number Generator(PRNG) within the range 0 to RAND_MAX. The RAND_MAX is a symbolic constant, depending on the C libraries its value greater but not less than 32767.
int rand(void);
The rand() function does not take any parameter.
The rand() function return value is an integer value between 0 and RAND_MAX.
Input | Return Value |
---|---|
success | integer value(0- RAND_MAX) |
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k, num;
time_t tm;
num = 5;
/* Intializes random number*/
srand((unsigned) time(&tm;));
/* random numbers from 0 to 49 */
for( k = 0 ; k < num ; k++ ) {
printf("%d\n", rand() % 50);
}
return(0);
}
Output:
38 45 29 29 47
#include <stdio.h>
#include <stdlib.h>
int main (){
printf (" Random number is: %d", rand());
printf ("\nRandom number is: %d", rand());
printf (" \n Random number is: %d", rand());
printf ("\n Random number is: %d", rand());
getch();
}
Output:
Random number is: 41 Random number is: 18467 Random number is: 6334 Random number is: 26500