srand() and rand() portability
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?
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement