C Reference function rand() generate a random number
This function of stdlib will generate a random number.
Usage of rand():
int rand (void);
Parameters:
The function rand() returns a pseudo-random integral number.
This number will be in the range 0 to RAND_MAX. The algorithm of rand() uses a seed to generate the series of numbers, this is why srand must be used to initialize the seed to some distinctive value.
The constant RAND_MAX is defined in standard library (stdlib).
The random numbers are delivered from a predetermined range.
Return value:
Will return an integer value between 0 and RAND_MAX.
Source code example of rand():
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ()
{
int number, input;
/* initialize random seed: */
srand ( time(NULL) );
/* Generate a random number: */
number = rand() % 10 + 1;
do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&input);
if (numberinput)
printf ("The number is higher\n");
} while (number!=input);
printf ("That is correct!\n");
return 0;
}
This entry was posted in C Reference stdlib.h Functions.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Tweet This! or use
to share this post with others.