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; Code:
ejspeiro@odjgomez-laptop:~/Escritorio/Tarea1/v-0.2$ ./main Code:
unsigned int xx; Code:
ejspeiro@odjgomez-laptop:~/Escritorio/Tarea1/v-0.2$ ./main 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! |
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:
[off-topic side note] Now that time() is mentioned, recently the unix time was: 1234567890. Code:
$ date -d @1234567890 |
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 Code:
/* The largest number rand will return (same as INT_MAX). */ Any guess? |
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
|
So its because of a standard difference?
|
Quote:
Just out of curiosity, why aren't you using another distribution such as normal or exponential? Kevin Barry |
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 |
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 |
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. |