Sign in to follow this  
Nukleon

[.net] Random Number generation over the network

Recommended Posts

Hello Everyone! I'm on the way to write a simple Space Strategy Game. To enhance replayability i would like to use randomly generated galaxies. No problem so far for a single player version, but i would like to make it multiplayer. The question that comes to mind is, when i seed the random number generator, will the sequence of numbers be the same on every! computer connected to my game? My Idea was that the server creates a seed for the galaxy and distributes it to the players. Then the galaxy would be constructed client side. For if the number generation would not be deterministic between individual systems, how can someone guarantee synchronisation? To reword my question in short, given a seed S will a random function in c# always and on all system returnn the same sequence of numbers? Thanks for your time. Nukleon [edit] Before you ask, I did search google,msdn and gamedev, but was not able to retrieve this particular piece of information. even after searching for a boost:random port to c# i did not find description for its networking behaviour

Share this post


Link to post
Share on other sites
That depends on how the rand function is implemented. Most likely, yes, it will. This is actually a pretty common technigue. Unless the function is truly random - it's polling some natural source of entropy - it's got to be deterministic. And if it really were truly random, it wouldn't need seeding!

Share this post


Link to post
Share on other sites
So it will be the same, even if users use different OS'es (but restrained to the Windows platform, as i will be using DirectX)

So we have user One
he has athlon cpu and runs windows xp
So we have user two
he has pentium cpu and runs windows 98se

The Server sends "GalaxySeed: 2"

Then User one does

Random fixRand = new Random( 2 );
for(int i=0;i<10;i++)
Console.Write( " {0,10} ", randObj.Next( ) );
and gets:
1 3 2 8 4 6 3 8 6 3 (arbitrarily chosen for demonstration)

and User two does
Random fixRand = new Random( 2 );
for(int i=0;i<10;i++)
Console.Write( " {0,10} ", randObj.Next( ) );
and gets:
1 3 2 8 4 6 3 8 6 3 (arbitrarily chosen for demonstration)

So they will essentially get the same numbers for the duration of the game, given that the seed will not change?
If that is correct, then thank you and i will begin writing the universe generator :-)

Nukleon

Share this post


Link to post
Share on other sites
The only way to be really sure that it will be platform-independent is to provide the RNG yourself. Try googling for it. There are lots of good (and less-than-good) algorithms available.

Share this post


Link to post
Share on other sites
Your idea is right. In fact, you will find that RNG really useful not only for galaxy creation but also when you compute decisions. If all the players and the server are all sync'ed by the same RNG then the server only neds to send the commands and allow the clients to develop the actions. As all have the same sequence, all will get the same outcome.

Luck!
Guimo

Share this post


Link to post
Share on other sites
BTW, as a remark, if same RNG algorithm is properly programmed, it should generate the same sequence, no matter the programming language. I'm currently advising some students for a cross-platform Windows/Linux/Cellphone game and the RNG was implemented in C++ and Java depending the platform with excellent results.

Luck!
Guimo

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Generating the world on the server and sending it (or parts of it) may not be possible, just because of the sheer amount of data. So if you could just send one number over the network and automatically generate the exact same world on two machines...

Most RTS games are peer-to-peer games, because of the enormous amounts of network communication that would be needed in a client-server setup. Even though the worlds are not generated, but loaded, the problems you have with running the exact same simulation on each machine are very similar as the originally stated problem.

The best way to make sure you generate the same worlds on each machine is to not use a real random function, but a pseudo-random stream. This thing is guaranteed to give you the same result every time, no matter what operating system you use. Be careful though, you have to make sure that the rest of the generation system is deterministic, so you have to look out for parts of the code that the compiler optimizes depending on the processor it runs on. In fact, I am not sure what the CLR does in this case.

Common problems with determinism are a result of:
- mixing deterministic and non-deterministic code
- uninitialized variables
- mis-use of pointers (for example sorting on them.. might not be as much of a problem with C#)
- using non deterministic random streams

What you should do is generate the galaxy/world, then CRC the whole thing and send the CRC over the network to make sure they are similar worlds. If they are not, pop up an error, and have them dump out the results to you can compare them and see where the error is. These bugs are amonst the hardest bugs I've ever encountered, so good luck! :)

- NickWaanders

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Windows provides a better random number generator in CryptGenRandom.

http://msdn.microsoft.com/library/en-us/seccrypto/security/cryptgenrandom.asp

Share this post


Link to post
Share on other sites

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