LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2009, 04:17 PM   #1
Bassy
Member
 
Registered: Oct 2004
Location: Venezuela
Distribution: Open SuSE v.-11.1.
Posts: 36

Rep: Reputation: 15
Talking 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! 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!
 
Old 03-29-2009, 04:54 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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

Last edited by Hko; 03-29-2009 at 04:57 PM.
 
Old 03-29-2009, 06:43 PM   #3
Bassy
Member
 
Registered: Oct 2004
Location: Venezuela
Distribution: Open SuSE v.-11.1.
Posts: 36

Original Poster
Rep: Reputation: 15
Red face 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?

Last edited by Bassy; 03-29-2009 at 06:45 PM.
 
Old 03-29-2009, 06:52 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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
 
Old 03-29-2009, 06:56 PM   #5
Bassy
Member
 
Registered: Oct 2004
Location: Venezuela
Distribution: Open SuSE v.-11.1.
Posts: 36

Original Poster
Rep: Reputation: 15
So its because of a standard difference?
 
Old 03-29-2009, 09:09 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Bassy View Post
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
 
Old 03-29-2009, 09:20 PM   #7
Bassy
Member
 
Registered: Oct 2004
Location: Venezuela
Distribution: Open SuSE v.-11.1.
Posts: 36

Original Poster
Rep: Reputation: 15
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
 
Old 03-29-2009, 09:33 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 03-29-2009, 09:39 PM   #9
Bassy
Member
 
Registered: Oct 2004
Location: Venezuela
Distribution: Open SuSE v.-11.1.
Posts: 36

Original Poster
Rep: Reputation: 15
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
 
  


Reply

Tags
algorithm, c++, generation, number, programing, random, time


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
random number generation rahul_kulkarni Linux - General 2 05-23-2008 01:23 AM
how is random playlist generation diffierent from windows winamp vs. xmms & amarok ? dateofexpiration Linux - Software 2 08-15-2007 07:46 AM
Random number generation jacques83 Programming 5 02-04-2006 12:38 PM
java- static messing with random number generation titanium_geek Programming 8 10-15-2005 01:39 PM
random number generation sailu_mvn Programming 4 03-10-2005 02:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration