Where is my mistake?

R

Red Dragon

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.


Correction,
Actually, srand has got problem as rightly pointed out and also as stated in the Q&A as seen below. In fact the authority does not recommend to call srand more than once during a run of a program which I dont understand why.

Rgds,
Khoon.

Question 13.17
Each time I run my program, I get the same sequence of numbers back from rand().


------------------------------------------------------------------------------

You can call srand to seed the pseudo-random number generator with a truly random initial value. Popular seed values are the time of day, or the elapsed time before the user presses a key (although keypress times are hard to determine portably; see question 19.37). (Note also that it's rarely useful to call srand more than once during a run of a program; in particular, don't try calling srand before each call to rand, in an attempt to get ``really random'' numbers.)

References: K&R2 Sec. 7.8.7 p. 168
ANSI Sec. 4.10.2.2
ISO Sec. 7.10.2.2
H&S Sec. 17.7 p. 393
 
P

pete

Red Dragon wrote:
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.

Maybe someday you'll understand about srand too.
 
W

William J. Leary Jr.

Red Dragon said:
Good news.
Problem solved.

No, you've just given the system time a chance to change between runs.
So the srand() seeding thing got blamed for nothing.

No, it *IS* the problem. As others have noted, srand seeds the random number
generator. It'll do it every time it's called. If you seed with the same
number, you'll get the same results to rand() calls. If you call it fast
enough, the system time will be the same for subsequent calls an the sequence
will repeat.

Try this:

/*
* Version to demonstrate problem with srand() call
* Based on:
* 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;

int i;

/*
* DEBUG Set range directly, don't give system time a chance to change
*
printf ("Please key in the Minimum and Maximum Value for the"
" Range of the two ");
printf ("\nrandom numbers> ");
scanf ( "%d %d", &MinRange, &MaxRange);
*/
MinRange = 1;
MaxRange = 10;
printf ( "MinRange =%d , MaxRange = %d\n", MinRange,MaxRange);

/* DEBUG and do it a several times */
for (i = 0; i < 5; i++)
{
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 ;
}

Here's the results:

C:\>x
MinRange =1 , MaxRange = 10
RndNum1 = 2, RndNum2= 3
RndNum1 = 2, RndNum2= 3
RndNum1 = 2, RndNum2= 3
RndNum1 = 2, RndNum2= 3
RndNum1 = 2, RndNum2= 3

But, if you call srand() only once, like this (or any number of other ways to
do it only once):

void GenRndNum (int x, int y, int *RndNum1,int *RndNum2)
{
static int run_once = 1;

if (run_once)
{
srand (time (NULL));
run_once = 0;
}
*RndNum1 = rand() % ((y+1 ) - x ) + x;
*RndNum2 = rand() % ((y+1 ) - x ) + x;
return ;
}

The results are:

C:\>x
MinRange =1 , MaxRange = 10
RndNum1 = 9, RndNum2= 2
RndNum1 = 10, RndNum2= 3
RndNum1 = 8, RndNum2= 1
RndNum1 = 4, RndNum2= 9
RndNum1 = 8, RndNum2= 6

If efficiency matters, then doing the srand() call in main (or somewhere else
executed only once per program run) will work better. Doing something like the
above hides srand() from the main program (possibly an advantage) and ensure
that it's called at least the once needed. The downside is that the test to
see if it's been done gets executed for every single invocation of GetRndNum.
I did it this way above mostly to keep from re-quoting the entire program just
to move srand() out of the function.

- Bill
 
R

Red Dragon

The results are:

C:\>x
MinRange =1 , MaxRange = 10
RndNum1 = 9, RndNum2= 2
RndNum1 = 10, RndNum2= 3
RndNum1 = 8, RndNum2= 1
RndNum1 = 4, RndNum2= 9
RndNum1 = 8, RndNum2= 6

If efficiency matters, then doing the srand() call in main (or somewhere
else
executed only once per program run) will work better. Doing something
like the
above hides srand() from the main program (possibly an advantage) and
ensure
that it's called at least the once needed. The downside is that the test
to
see if it's been done gets executed for every single invocation of
GetRndNum.
I did it this way above mostly to keep from re-quoting the entire program
just
to move srand() out of the function.

- Bill
Thanks Bill,
I can see that you are an expert in this field. I tried and it worked
exactly as what you have explained. I have benefited greatly from your
contribution and others too.
Very grateful that you have come in to clear the haze.
Regards,
Khoon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top