Sign in to follow this  

srand() and rand() portability

This topic is 4261 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

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 this post


Link to post
Share on other sites
You could easily just obtain and use the code for a common prng, and explicitly use that instead:
//In .cpp
long holdrand;

//In .hpp
extern 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 4261 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this