From: Oscar F. <of...@wa...> - 2002-11-30 20:01:39
|
"/^GHS^\\\\ SPhiNX" <sf...@te...> writes: > Well, very strange. > > I have this program: > > > #include <stdio.h> > #include <stdlib.h> > > int main(void) > { > int a; > srand(time(NULL)); > a = rand(); > printf("a has the value %i.\n",a); > return(0); > } > > > If I compile this in gcc 3.0 under linux, everything works great, > the numbers are totally random. Errr... no, they aren't. They are pseudorandom, which means that the sequence seems random unless you look at it "close enough". For generating true random numbers, the computer needs some aid from the external world. > But if I compile it with gcc from MinGW 2.0.0-3 the numbers grow > exactly linked to the proceding time. They start somewhere at 17051 > (currently) and usually grow by increments of 3 or 4, so if I > execute the program quickly one after another, I have one or two > times the same number, then 3 or 4 is added and so on. > > Why do I get this behaviour? As you know, srand() sets the "seed" number which determines the sequence of "random" numbers to be generated. If you use the same seed, you see the same sequence. Hence, if you re-run your program fast enough (within the same second of the previous run) the seed will be the same. The characteristics of the pseudo-random number generator implemented on MSVCRT explains why the first number generated by one run is close from the first number generated on the next run. That is, similar seeds produces similar first numbers. As you just discovered, the glibc/Linux random generator is better. > And could anyone tell me, how I could reply to this message without > starting a new one, I mean, how can I append messages to this > thread? If your mail program is smart enough, just choosing "Reply" will do the trick. -- Oscar |