srand() and rand() portability

Recommended Posts

Of course they are standards so they are available where sdtio is but I was thinking on adding replays to my game, the only problem is that some objects, specially the monsters' AI rely on random numbers. So the question is : would equal srand() values generate the same results for rand() in different platforms? and if not, is there a library that allows this?

Share on other sites
No, they don't.

Mersenne Twister gets my vote as an alternative.

Share on other sites
If you're using C++ then boost::random is probably the place to go.

Share on other sites
I personally like ISAAC best.

Share on other sites
thanks very much with 3 alternatives I guess I have plenty of options

Share on other sites
You could easily just obtain and use the code for a common prng, and explicitly use that instead:
//In .cpplong holdrand;//In .hppextern long holdrand;inline void srand(long seed){	holdrand = seed;}inline int rand(void){	return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);}
I'm not going to pretend it is the best prng in the world, but it is usually good enough (and very fast), and it is probably identical to what you are already using.

Share on other sites
I vote for boost::random, too. Remember that rand() and other implementations like iMalcs aren't reentrant, so for multi-threaded apps they should be avoided anyway.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627653
• Total Posts
2978440

• 10
• 12
• 22
• 13
• 33