Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Where is my mistake? (http://www.velocityreviews.com/forums/t439778-where-is-my-mistake.html)

 Red Dragon 10-13-2005 12:32 PM

Where is my mistake?

I am self study C program student.
Hope someone can help me with this problem.
This program generates random numbers over a user defined range using call function
I used the call function " GenRndNum". The range is 2 and 10.
The problem is that I get the same 2 random numbers generated over 2 calls.
I should get 2 different random numbers. Can someone please point out my mistake?
Thanks
Khoon.

/* Generation of Random Numbers over a User Defined Range.*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int GenRndNum (int x, int y);

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);
RndNum1= GenRndNum (MinRange, MaxRange);
printf ("RndNum1 = %d\n",RndNum1);

RndNum2= GenRndNum (MinRange, MaxRange);
printf ("RndNum2 = %d\n",RndNum2);

return 0;
}

int GenRndNum (int x, int y )

{ int z;
srand (time(NULL));
printf ("x = %d, y = %d\n",x,y);

z = rand() % ((y+1 ) - x ) + x;
printf ("z = %d\n",z);

return (z);
}

/*-----------------------
Result of Output:

Please key in the Minimum and Maximum Value for the Range of the two
random numbers> 2 10
x = 2, y = 10
z = 5
RndNum1 = 5
x = 2, y = 10
z = 5
RndNum2 = 5
Press any key to continue_
*/

 Skarmander 10-13-2005 12:37 PM

Re: Where is my mistake?

Red Dragon wrote:
> I am self study C program student.
> Hope someone can help me with this problem.

<snip>

Please do not post HTML to Usenet. Many clients can't handle this and
even those who can have trouble converting it to text when replying.

You're using Outlook Express. To change the format, choose Tools ->
Options, click on the Send tab, and make sure the radio button "Plain
Text" is on for "News Sending Format".

Rest of the message ignored, sorry.

S.

 Eric Sosman 10-13-2005 12:44 PM

Re: Where is my mistake?

Red Dragon wrote:
> [...]
> The problem is that I get the same 2 random numbers generated over 2 calls.
> I should get 2 different random numbers. Can someone please point out my mistake?
> [...]
> int GenRndNum (int x, int y )
>
> { int z;
> srand (time(NULL));

Here it is. See Question 13.17 in the comp.lang.c
Frequently Asked Questions (FAQ) list at

http://www.eskimo.com/~scs/C-faq/top.html

In fact, see all of Questions 13.15 through 13.20; there
are a few other problems with what you're doing, and the

> printf ("x = %d, y = %d\n",x,y);
>
> z = rand() % ((y+1 ) - x ) + x;
> printf ("z = %d\n",z);
>
> return (z);
> }

--
Eric Sosman
esosman@acm-dot-org.invalid

 orium69@gmail.com 10-13-2005 04:55 PM

Re: Where is my mistake?

you are using always the same srand value. try this:

long tm;
time(&tm);
srand(tm);

 Christopher Benson-Manica 10-13-2005 05:00 PM

Re: Where is my mistake?

orium69@gmail.com wrote:

> you are using always the same srand value. try this:

It is proper Usenet etiquette to include the relevant portions of the text
instructions below, penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

 Eric Sosman 10-13-2005 05:10 PM

Re: Where is my mistake?

orium69@gmail.com wrote On 10/13/05 12:55,:
> you are using always the same srand value. try this:

Do NOT try this!

> long tm;
> time(&tm);
> srand(tm);

Reason: The argument to time(), if non-NULL, must be
a pointer to a `time_t' object. `time_t' and `long'
are not necessarily the same thing. If you lie to
the compiler, it will get its revenge.

--
Eric.Sosman@sun.com

 Dale 10-13-2005 06:04 PM

Re: Where is my mistake?

"Red Dragon" <tskhoon@streamyx.com> wrote in
news:434e53eb\$1_1@news.tm.net.my:
>
> I am self study C program student.
> Hope someone can help me with this problem.
> This program generates random numbers over a user defined range using
> call function I used the call function " GenRndNum". The range is 2
> and 10. The problem is that I get the same 2 random numbers generated
> over 2 calls. I should get 2 different random numbers. Can someone
> please point out my mistake? Thanks
> Khoon.
>
>
> /* Generation of Random Numbers over a User Defined Range.*/
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
> int GenRndNum (int x, int y);
>
> 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);
> RndNum1= GenRndNum (MinRange, MaxRange);
> printf ("RndNum1 = %d\n",RndNum1);
>
> RndNum2= GenRndNum (MinRange, MaxRange);
> printf ("RndNum2 = %d\n",RndNum2);
>
> return 0;
> }
>
> int GenRndNum (int x, int y )
>
> { int z;
> srand (time(NULL));
> printf ("x = %d, y = %d\n",x,y);
>
> z = rand() % ((y+1 ) - x ) + x;
> printf ("z = %d\n",z);
>
> return (z);
> }
>
>
> /*-----------------------
> Result of Output:
>
> Please key in the Minimum and Maximum Value for the Range of the two
> random numbers> 2 10
> x = 2, y = 10
> z = 5
> RndNum1 = 5
> x = 2, y = 10
> z = 5
> RndNum2 = 5
> Press any key to continue_
> */

Less than a second passes between the two calls to time(), so it's
returning rhe same value to srand() each time.

One way to fix this would be to put a 1-second delay in between the calls
to GenRndNum. Some compilers support a sleep() function. If yours
doesn't, then this code will provide a 1-second delay:

time_t t;
t = time(NULL);
while(time(NULL) <= t);

Put it in GenRndNum(), right before the call to srand(). This will
guarantee that srand() will always get a different seed value.

 Anonymous 7843 10-13-2005 06:11 PM

Re: Where is my mistake?

In article <1129222528.802184.296920@f14g2000cwb.googlegroups .com>,
<orium69@gmail.com> wrote:
>
> you are using always the same srand value. try this:
>
> long tm; /* should be time_t as pointed out by another helpful person */
> time(&tm);
> srand(tm);

Aside from being wrong (as if this weren't enough) this
approach to seeding a random number generator opens up a
form of attack where two instances of the program are
started at the same time. One instance is used to observe
the seed, the second is manipulated by the user based on
his knowledge of the seed obtained from the first
instance.

For example, two instances of a card playing program
started at once would have the same sequence of cards
generated. The user could anticipate each card and play
the 2nd instance with complete knowledge of the cards
before they appear. In many card games this gives an

There are many ways to solve this problem, however most of
them rely on OS-specific features. Of course, whatever you
do can be undone by a hacker willing to modify the program,
e.g. overwriting the call to srand() with a NO-OP
instruction.

 Barry 10-13-2005 06:43 PM

Re: Where is my mistake?

"Anonymous 7843" <anon7843@example.com> wrote in message
> In article <1129222528.802184.296920@f14g2000cwb.googlegroups .com>,
> <orium69@gmail.com> wrote:
> >
> > you are using always the same srand value. try this:
> >
> > long tm; /* should be time_t as pointed out by another helpful person

*/
> > time(&tm);
> > srand(tm);

>
> Aside from being wrong (as if this weren't enough) this
> approach to seeding a random number generator opens up a
> form of attack where two instances of the program are
> started at the same time. One instance is used to observe
> the seed, the second is manipulated by the user based on
> his knowledge of the seed obtained from the first
> instance.
>
> For example, two instances of a card playing program
> started at once would have the same sequence of cards
> generated. The user could anticipate each card and play
> the 2nd instance with complete knowledge of the cards
> before they appear. In many card games this gives an
>
> There are many ways to solve this problem, however most of
> them rely on OS-specific features. Of course, whatever you
> do can be undone by a hacker willing to modify the program,
> e.g. overwriting the call to srand() with a NO-OP
> instruction.

OT As all of this has been. If you have a system in which this is a
concern, you have much bigger security among other, concerns than a
pseudo-random number generator

 Keith Thompson 10-13-2005 07:04 PM

Re: Where is my mistake?

Eric Sosman <eric.sosman@sun.com> writes:
> orium69@gmail.com wrote On 10/13/05 12:55,:
>> you are using always the same srand value. try this:

>
> Do NOT try this!
>
>> long tm;
>> time(&tm);
>> srand(tm);

>
> Reason: The argument to time(), if non-NULL, must be
> a pointer to a `time_t' object. `time_t' and `long'
> are not necessarily the same thing. If you lie to
> the compiler, it will get its revenge.

This shouldn't be a case of lying to the compiler. If time_t happens
to be long, the code will work (though of course it's non-portable).
If time_t isn't long, the compiler *should* issue a diagnostic on the
call to time(), something like "passing arg 1 of `time' from
incompatible pointer type".

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.

All times are GMT. The time now is 10:29 PM.