This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Random Number Generator


Hi!

Monday, 30 April, 2001 HRahman10@aol.com HRahman10@aol.com wrote:

Hac> Hi!, I'm writing a program using the random number genrator
Hac> rand() defined in <stdlib.h>.  I've written the following program
Hac> to test it:   

Hac> void main()
Hac> {
Hac>   int input1, i;
Hac>   srand(time(NULL));
Hac>   for (i=0; i<20 ; i++)
Hac>     {
Hac>       input1 = rand() * 10 / (RAND_MAX + 1);
Hac>       printf("random numbers %i\n",input1);
Hac>     }
Hac> } 


Hac> BUT, the results I get are all zeros.  Is there something wrong
Hac> with the gcc compiler provided by cygwin, because I've tested
Hac> this program at university, and it outputs random numbers
Hac> correctly.   

your program is wrong. RAND_MAX == INT_MAX == 0x7fffffff on cygwin.
so you've got integer overflow, RAND_MAX + 1 == -2147483648

Egor.            mailto:deo@logos-m.ru ICQ 5165414 FidoNet 2:5020/496.19



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]