On Mon, Apr 30, 2001 at 02:13:08PM -0400, Larry Hall (RFK Partners, Inc) wrote:
> At 01:37 PM 4/30/2001, you wrote:

> >void main()

At the risk of being accused of pendantry, main() should be declared as
int.

> >{
> >   int input1, i;
> >
> >   srand(time(NULL));
> >   
> >   for (i=0; i<20 ; i++)
> >     {
> >       input1 = rand() * 10 / (RAND_MAX + 1);

Here's your problem.  You're doing integer math.  10/(RAND_MAX + 1) is
going to be 0.  Think about why . . you're dividing a small integer by a
rather large integer.

Moreover, RAND_MAX is usually an alias for INT_MAX.  Therefore RAND_MAX
+ 1 is an overflow condition.  

Moreover, you don't want the +1 in the denominator.  You want rand()'s
proportion of RAND_MAX--which (should be) a uniform deviate, multiplied
by 10, to bring its range into 0 < 9, and THEN add 1, which will (after
truncation) give you numbers between 1 and 10.

I think what you really want is something like this:

          input1 = (double)rand() * 10.0/((double)RAND_MAX) + 1.0;


> >       printf("random numbers %i\n",input1);
> >
> >     }
> >} 
> Try the latest snapshot at www.cygwin.com.

The OP`s problem was semantic.  He was specifying integer math, but
expecting results of double math.  According to integer math, his result
is correct . . though not what he intended.


Charles

-- 
Charles Krug, Jr.
Applications Engineer
Pentek Corp
1 Park Way
Upper Saddle River, NJ 07458


--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

Reply via email to