Mail Archives: djgpp/1998/09/22/22:15:28
On Tue, 22 Sep 1998 14:46:20 -0500, "Paul Minerva" <p_minerva AT msn DOT com>
wrote:
>I am writing an application that needs the rand() function. When I compile
>this program using the UNIX g++ it works fine. When I compile the program
>using gcc, it does not seem to work correctly.
>Any idea what the problem is?
What does it mean that your snippet
#include <stdio.h>
#include <stdlib.h>
main() {
int r = rand();
printf("%d\n",r);
return r;
}
does not seem to work correctly ? Are you complaining that it will
always return 0 ? This is perfectly correct. Every rand() will always
return the same number after the first call to rand() in a program.
Sometimes it is 123456782, sometimes it is 0.
If you want "unpredictable" random numbers you may call
#include <time.h>
srand(time(0));
_once_ in your program before calling rand() for the first time.
Regards
Horst
- Raw text -