Skip to main content
GameDev.net gamedev.net
🔒 Locked

srand() and rand() portability

Started by Vexorian Apr 15, 2006 at 7:15 AM 5 replies 2.4k views
Original Post
Vexorian
Vexorian
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
smart_idiot
smart_idiot
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
Vexorian
Vexorian
thanks very much with 3 alternatives I guess I have plenty of options
------ XYE - A new edition of the classic Kye
iMalc
iMalc
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.
etothex
etothex
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.