Randoms

Started by
9 comments, last by Axehandler 22 years, 4 months ago
I was curious.. If I random a number in Delphi... and seed it with (1111) NOT time. I would as example get a random number of 1234567..... and since I keep the same seed.. each time I run that program the same "random" number will appear. Here's the question... is it the same for all languages? Example is C++ if I seed like this.. will I get the SAME 1234567 as I would in Delphi? (excuse the lack of coding knowledge) srand( 1111 ); printf( " %6d\n", rand() ); Same goes for Java.... Seeding the Same... Will it generate the same "RANDOM" number? Axehandler Edited by - Axehandler on December 14, 2001 1:04:23 PM
Advertisement
No, the exact algorithm used to generate pseudorandom numbers isn't part of the language or library specifications.

You can read up several RNGs implementations in chapter 7 of Numerical Recipes (available online at www.nr.com). If you want repeatable pseudorandom numbers, use one such implementation.

Link: Go to http://lib-www.lanl.gov/numerical/bookcpdf/c7-1.pdf. It also explains the theory behind rngs.

Edited by - Fruny on December 14, 2001 1:22:01 PM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Right, the algorithm will be different across languages, but in general (AFAIK, please feel free to post counter-examples) random number generators are deterministic, so the same seed will always produce the same sequence for a given algorithm.

That is to say, rand (5) in Java and rand (5) in Delphi might give you different patterns, but it will always give you the same pattern using the same library.
quote:Original post by Axehandler
each time I run that program the same "random" number will appear.


Not if you write the line...
Randomize; 
...beforehand. If you miss this line out, the program will generate the same random numbers each time it runs.



~ There's no substitute for failure ~

Edited by - MarkyD on December 14, 2001 1:23:05 PM
well I''m trying to duplicate a Random generated from a Java App but generate the same random numbers on like c++ or delphi or something...

Looks like if I want to generate the same number I would need to just use Java for the 2nd app as well... wanted more speed thou =(

Any other idea''s? =)

Axe
quote:Original post by MarkyD
Not if you write the line...
Randomize;  

...beforehand. If you miss this line out, the program will generate the same random numbers each time it runs.


You know, actually, the game ''Elite'' used the fact that RNGs were deterministic (as Stoffel correctly stated) to generate a virtually infinite universe. Either the game was seeded with an initial value and each planet corresponded to a known number of rand() calls before generating the planet data, or the planet''s "coordinates" were used as a seed. That way, the data needn''t be kept in memory which was rather scarce, at the time.

Repeatability can be a feature.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Axehandler
well I'm trying to duplicate a Random generated from a Java App but generate the same random numbers on like c++ or delphi or something...

Looks like if I want to generate the same number I would need to just use Java for the 2nd app as well... wanted more speed thou =(

Any other idea's? =)

Axe


Yes, go fetch a rng algorithm there and use it in both apps...

People, they just don't listen



Edited by - Fruny on December 14, 2001 1:29:20 PM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
LOL the JAVA app isn''t mine no source...

Axe
Ok, please accept my humble apologies.

You can try to reverse engineer it : assume it is a linear-congruence RNG, seed it with 0, 1, ... n and try to fill in the parameters of the equation (see in N-R, again ).

If it is not a linear congruence RNG, well, tough luck.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
new way of generating random numbers is to get the temperature of a transister on your motherboard as it is governed by chaos theorum... unique every time. The other is to multiply the millisecond counter of you clock by a constant appropriate with the range required.
I burn cold

This topic is closed to new replies.

Advertisement