Use rand function example given in msdn or one rerence which i gave in response to qs "Random number output to text file" here...anyway i repeat
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(int argc, char* argv[])
{
int i;
FILE *fp;
fp=fopen("c:\\newtext.txt","w+"
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
To elaborate, a call to rand() generates psuedo-random numbers, or "random" numbers that follow a mathmatical pattern. This, of course, isn't truly random, so a "seed" is thrown in to change the pattern of rand() to appear not so random. srand() accmoplishes this, and by passing an ever-changing arguement like system time to srand ( srand( time( 0 ) ) ) you get the effect of actual random numbers.
rand() "returns a value between(inclusive) 0 and and RAND_MAX (defined by the compiler, often 32767). " so to get a random number between a certain range say 0-10, you need to use the modulus operator to shrink the range: rand() % 11. If you want to get a random number between, say 5 and 15 you simply do as above, but use an offset ( 5 in this case so... rand % 11 + 5 ).
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.