Jump to content
  • Advertisement
Sign in to follow this  
CodeMunkie

Portability of results of srand and rand

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

I am working on a multiplayer networked game, written in C++, that generates a fair amount of random content. My initial thought was that when a new player connects, the server could just send the player its random seed, and then the client can plug that into srand. In theory that should mean that the server and all clients generate the exact same "random" content since they are all using the same random seed, and it reduces the amount of serialization that needs to happen whenever random entities are generated. The game is intended to be cross platform across Windows, Linux, and (if I can get my hands on one to do a build) Mac. I have only tested the code on Windows machines with very similar CPU architectures, so I haven't seen any problems yet. However, I am concerned that there may be some serious gotcha waiting to bite that simply has not been exposed yet. So the question is, given the same seed for srand(), is rand() guaranteed to generate the same value on any platform? If not, is there a pseudo-random number generator that has this guaranteed behavior?

Share this post


Link to post
Share on other sites
Advertisement
Although I haven't the answer for rand() / srand() you have the option to choose your own algorithm for random numbers and thus you wouldn't have to worry about it. One example would be the mersenne twister: Wikipedia , boost/random and the home page .

Share this post


Link to post
Share on other sites
The short answer to the other half of the question is no. Every compiler is free to implement rand however they want, and it's very likely that each compiler's standard library will use a different generator. If you want to have guaranteed behavior across machines, just use your own generator (either something like boost's, some standard mersenne-twister code, or even just a simple linear congruential).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!