> In your original program, upthread, you call srand() withing
> GenRndNum(), and you call GenRndNum() in main() -- thus two calls to
> srand(). Since the value returned by time(NULL) is unlikely to change
> between the two calls, it's not surprising that you'd get the same
> "random" number twice. (It's also a bug; srand() should normally be
> called only once in a given program, unless you're deliberately
> recreating the same sequence multiple times.)
>
> In the program you posted here, in spite of what you wrote above,
> there's only one call to srand().
>
> If you change your original program (the one with the GenRndNum()
> function) so it calls srand() once in main(), rather than calling it
> from GenRndNum(), it should do what you want.
>
> --
> Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.
Good news.
Problem solved.
Earlier I did not understand the passing of data and wrote the program wrongly.
I have now discovered my error in the structure of the program writing.
Now I have rewritten and the program is working perfectly. I am getting 2 different random numbers each time now.
So the srand() seeding thing got blamed for nothing.
Here is my correct program.
Rgds,
Khoon.
/* Generation of Random Numbers over a User Defined Range with Function.
15.10.05 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void GenRndNum (int x, int y, int *RndNum1, int *RndNum2);
int main (void)
{
int MinRange;
int MaxRange;
int RndNum1;
int RndNum2;
printf ("Please key in the Minimum and Maximum Value for the Range of the two ");
printf ("\nrandom numbers> ");
scanf ( "%d %d", &MinRange, &MaxRange);
printf ( "MinRange =%d , MaxRange = %d\n", MinRange,MaxRange);
GenRndNum (MinRange, MaxRange, &RndNum1, &RndNum2);
printf ("RndNum1 = %d, RndNum2= %d\n",RndNum1,RndNum2);
return 0;
}
void GenRndNum (int x, int y, int *RndNum1,int *RndNum2)
{
srand (time (NULL));
*RndNum1 = rand() % ((y+1 ) - x ) + x;
*RndNum2 = rand() % ((y+1 ) - x ) + x;
return ;
}