srand() and rand() portability

Started by
5 comments, last by etothex 18 years ago
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?
------ XYE - A new edition of the classic Kye
Advertisement
No, they don't.

Mersenne Twister gets my vote as an alternative.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
If you're using C++ then boost::random is probably the place to go.
-Mike
I personally like ISAAC best.
thanks very much with 3 alternatives I guess I have plenty of options
------ XYE - A new edition of the classic Kye
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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