#### Archived

This topic is now archived and is closed to further replies.

# Randoms

This topic is 6126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
LOL the JAVA app isn''t mine no source...

Axe

##### Share on other sites
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.

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 33
• ### Forum Statistics

• Total Topics
632634
• Total Posts
3007546
• ### Who's Online (See full list)

There are no registered users currently online

×