Archived

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

Random number generation

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the random number generator functions (rand and srand) to generate random numbers. The numbers are used to generate a random terrain. If I seed the the random numbers using a known seed (using srand) will the random numbers generated aways be the same across all windows platforms (win95, 98 etc..)? It always appears to generate the same results on 2000. I am planning to use this for a multiplayer network game to ensure all of the terrains across the computers are identical. thanks for the Help Gavin. I am using MS Visual Studio .NET as the compiler

Share this post


Link to post
Share on other sites
> If I seed the the random numbers using a known seed (using
> srand) will the random numbers generated aways be the same
> across all windows platforms (win95, 98 etc..)? It always
> appears to generate the same results on 2000.

Don''t know. And if I don''t know I roll my own.

The source for rand is simple and widely available, & you can just use a version of it in your own code. This has the advantages:

- more easily debuggable
- easy to try substitutes (as standard rand() has some known problems)
- can produce much faster code, as it is short enough to be inlined
- avoids nasty problem where your use of rand() is influenced by someone elses, e.g. if someone calls it from a library or in sample code you use.

As long as you stick to integer arithmetic you can produce code that works identically on all imaginable game platforms.

Share this post


Link to post
Share on other sites
Probably they will be same as long you use the same executeable, but I wouldn''t rely on it.
You never know how those library functions are implemented, they can suddenly change on windows2015 or whatever.

Also they will probably change if you use another compiler.

The best solution will be you write your own random number generator or use one of those that can be found in the net.
As that pice is completly in your code you have complete control over it, and you even rely it will be "constant random" even over different platforms.

Share this post


Link to post
Share on other sites
This should be in the general programming forum; you''d have a better chance of getting a good answer there.

I don''t know either, but I think that it will probably always be the same. Use boost if you are not sure.

Cédric

Share this post


Link to post
Share on other sites
It''s not part of the library spec that the same seed produces the same output when you switch library versions (wouldn''t have anything to do with the OS, BTW) so you have to assume it could be either different or the same--that is, you can''t count on getting the same output, and you can''t count on getting different output. Roll your own.

Share this post


Link to post
Share on other sites
OTOH, even if it''s not part of the spec, I don''t see how it could be different from one computer to another. It''s a pseudo-random number generator. It is not going to use a special "rnd" function if it is supported by the processor.

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
OTOH, even if it''s not part of the spec, I don''t see how it could be different from one computer to another. It''s a pseudo-random number generator. It is not going to use a special "rnd" function if it is supported by the processor.



Different versions of a library may use different constants or a different algorithm altogether, thus changing the output for a given seed.

If you''re using the CRT as a shared library (i.e. DLL) then you''d be insane to assume the outputs will be the same. If you using it as a static library, then that assumption isn''t as bad...

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
That may be true, but that is not what the OP was looking for. He does not intend (AFAICT) to compile his code on multiple compilers/platforms. He just wants to know if his .exe will return the same random numbers on any machine. I think that it will.

Cédric

Share this post


Link to post
Share on other sites
Still it would depend on whether your rtl is statically or dynamically linked. If you have a dynamically linked rtl then another application using the same compiler, highly likely, could replace the rtl with a newer version. Admittedly the random number generator is highly unlikely to change. Even in your own application you may need to distribute a newer version of the rtl to fix a problem not of your own making. So most likely the best is to copy the source for the rtl routine and just call it if you want to be absolutely sure it doesn''t change.

Share this post


Link to post
Share on other sites