Random number sync between server and client (709742)

Hello,
I’m learning how to use UNet and at the moment I’m just trying to build some simple samples that will help in the long run on the development of my application. Right now, I have a simple scene with 2 players and a text field where I feed random numbers that updates every 2 seconds. The problem that I have is that the numbers are always different between client/server or server/client/client.

This is a good example of when to use a syncvar. Have the server generate the random number and store it in a syncvar. The value will then get pushed out to all clients. Have the clients use a hook function to set the text field to the new value. https://docs.unity3d.com/ScriptReference/Networking.SyncVarAttribute.html

1 Like

Get the random number only on the server. Set it to a syncvar. Any client just checks the syncvar, and does not generate its own random number.

1 Like

Alternatively you could let the server generate a seed, send the seed over to all clients, have the clients use this seed for their random numbers generator, and they should then generate the same results as the server every time (as long as they generate the same amount of numbers, else they’ll go out of sync).
The advantage of this would be that if you need to generate a lot of random numbers at once (for example hundreds of units rolling for critical damage) you don’t need to send all their results from server to all the clients. Instead you just tell the clients to roll X amount of random numbers, and you’ll get the same result.
Someone correct me if I’m wrong with that here, but I believe that’s how RTS games for example solve this issue.

I too was going to suggest a seed. I’m confused why others would recommend syncing the variable, doesn’t that just introduce extra networking? If you introduce a seed value on connect, then RNG should be deterministic.

I suppose it’s a matter of scale and convenience. If you’re having few networked objects that synchronise their state constantly, like in a multiplayer FPS, it makes little sense to extract your RNG-based calculations from this synchronization. Just let the server handle the calculations and send the results over to all clients.
This becomes less feasible when you’re dealing with many objects at once, like in an RTS game, where it makes more sense the entire game is deterministic and only synchronize player inputs over the net (this is called lock-step).
In that case determining a random seed and synchronizing that at the start of the game would make perfect sense.
I’m mostly just suggesting it as an alternative, in case it could be useful.

I’ve tried the “send the seed” path before and it is fraught with peril. Ensuring that all your other code is deterministic, and that all your calls to the RNG are done in the same order is very very hard. This was for a simple turn-based card game, and it became untenable for me. Now, maybe I’m just not good enough to make this work (possible!), but the downsides outweighed the upsides. YMMV, IMHO, etc etc. Good Luck!