LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Random number generation weird behavior in Win && Linux comparation (https://www.linuxquestions.org/questions/programming-9/random-number-generation-weird-behavior-in-win-and-and-linux-comparation-715338/)

Bassy 03-29-2009 04:17 PM

Random number generation weird behavior in Win && Linux comparation
 
Hallo!

I'm currently working with Genetic Algorithms which led me to work with Random Numbers Generation but I started to write my code in Windows XP.

I wrote this very simple code for this task:

Code:

unsigned int xx;

for(ii = 0; ii < pob_size; ii++){
  srand((unsigned int) time(NULL));
  xx = ((unsigned int) rand());
}

In Windows this worked but when I tried on Ubuntu v.-8.04. Every iteration led me to the same number for xx:

Code:

ejspeiro@odjgomez-laptop:~/Escritorio/Tarea1/v-0.2$ ./main
938948534
938948534
938948534
938948534
938948534
938948534
938948534
938948534
938948534
938948534

Now, I made this little change:

Code:

unsigned int xx;

srand((unsigned int) time(NULL));
for(ii = 0; ii < pob_size; ii++){
  xx = ((unsigned int) rand());
}

And as you can see here... it worked!

Code:

ejspeiro@odjgomez-laptop:~/Escritorio/Tarea1/v-0.2$ ./main
3472508823
4275268834
971400928
973015353
4290163366
4118231445
1031671263
2147356400
4156501942
1735389272

Now... teach me guys! Please! :D Why is this happening!?!

I know I should be searching on my own but since its already working I'll keep focusing on my GA to make it better!

Danke!

Hko 03-29-2009 04:54 PM

srand() initializes the random generator to start a certain point in the (long) "list of random numbers".

If you start your program with e.g: srand(19369) you will get the same series of random numbers every time the program runs. This can be handy e.g. for some tests or debugging.

So to get different random numbers each time the program runs, time() is used to initialize the random generator. And the random numbers you get are (to a certain extend) unpredictable. time() returns the number of seconds since 1/1/1970.

In your program you call srand(time()) each time in the loop. Every time the loop runs within one second, time() will return the same number. So you're then initializing the random generator to the same starting point every tim just before you call random().

The right spot to call srand(time()) is at the start of your program. Only once.

I don't the reason why this doesn't work like that under Windows. Possible causes could be:
  • Windows ignores the number passed to srand()
  • Under Windows time() returns milliseconds instead of seconds under linux.
  • Windows not fast enough to rum the loop within a second. :-)


[off-topic side note]

Now that time() is mentioned, recently the unix time was: 1234567890.
Code:

$ date -d @1234567890
Sa 14. Feb 00:31:30 CET 2009


Bassy 03-29-2009 06:43 PM

A great difference among the same declarations
 
Jejeje nice answer but check this out:

On my Windows XP I have this declared inside stdlib.h:

Code:

#define RAND_MAX        32767U
but in Ubuntu v.-8.04 its declared like this:

Code:

/* The largest number rand will return (same as INT_MAX). */
#define        RAND_MAX        2147483647

Wow! Its a great difference but I'm not sure about its implications.

Any guess?

dmail 03-29-2009 06:52 PM

The reason you are seeing different values for RAND_MAX is due to its value being implementation defined, yet it guaranteed to have a value of at least 32767. See C99 7.20.2.1.5

Bassy 03-29-2009 06:56 PM

So its because of a standard difference?

ta0kira 03-29-2009 09:09 PM

Quote:

Originally Posted by Bassy (Post 3491982)
So its because of a standard difference?

That just means C99 imposes a minimum but glibc exceeds it.

Just out of curiosity, why aren't you using another distribution such as normal or exponential?
Kevin Barry

Bassy 03-29-2009 09:20 PM

I'm writing the GA to maximize a two dimensional function f (without restrictions).

My population therefore is a set of vectors in IR square so I'm simply initializing this population uniformly distributed. The reason is that this is an introductory course in AI so I guess we're just trying to put aside the probabilistic specs to focus on how the GA works.

But hey! Have you worked with these algorithms using these other distributions? What would I see different?

PS: A LaTeX front-end is needed here XD

ta0kira 03-29-2009 09:33 PM

Your values would follow whatever distribution you're using, i.e. if you measured the stats of a large enough sample of the random values you'd end up with the parameters of the distribution you're using. With a uniform distribution, however, every value should occur equally. I don't know much about GA or AI, but I'm pretty sure genes express themselves in some sort of exponential distribution, i.e. a few genes are used for protein synthesis an exponentially-greater amount than almost all the others. That means if you were randomly selecting a gene to synthesize a protein with, 99% of the time you'll end up with one of the same 5% of the possibilities (or whatever you tune your parameters to.) Don't take my word for it; I haven't studied AI so I don't know what exactly you're doing. For all I know, the randomization you're talking about has little to do with the actual process other than that you don't want the same results every run. This is probably just an uneducated rant about something I think I know something about.
Kevin Barry

Bassy 03-29-2009 09:39 PM

XD Thanks but:

A lame (but simple) answer on my behalf would be this:

Theory on GA states that uniform distribution should be used. This is because the idea is that the whole search domain has equally probability of being initialized.

The cool question would be if changing my distribution given the particular problem (optimization) would help better :P


All times are GMT -5. The time now is 03:20 PM.