Riff

Shared random seed

Some games need every player to produce the same random results without sending each result over the network: card shuffles, loot rolls, map generation, enemy spawns. Riff supports this with a seed stored in the room's properties and a deterministic generator in the Unity SDK (NetworkRandom).

How it works

  1. The player who creates the room puts a number in the room's custom properties under the key randomSeed.
  2. Every player who joins receives the room's properties in roomJoined.
  3. The SDK sees randomSeed and initialises client.Random with it, automatically.
  4. All clients now have the same generator in the same state. The same sequence of calls gives the same numbers everywhere.

Riff does not pick the seed for you. It only stores and distributes whatever the creator put in the properties, so the seed is created by your game (the master) at room creation:

var options = new RoomOptions {
    CustomProperties = new Dictionary<string, object> {
        { "randomSeed", UnityEngine.Random.Range(1, int.MaxValue) }
    }
};
await client.JoinRoomAsync("arena-1", options);

Remember options only apply when the room is created: if the room already exists, the seed already there is the one everybody gets.

Using it

var rng = client.Random;          // or RelayManager.Instance.Random
if (rng.IsInitialized) {
    int roll    = rng.Next(1, 7);        // 1..6
    float f     = rng.NextFloat();       // 0.0 .. <1.0
    bool coin   = rng.NextBool();
    rng.Shuffle(deck);                   // in-place Fisher-Yates
}
MethodReturns
Initialize(int seed)(re)start the generator from a seed
Next()non-negative int
Next(max)int in [0, max)
Next(min, max)int in [min, max)
NextFloat()float in [0, 1)
NextBool()bool
Shuffle(list)shuffles a list in place
Seed, IsInitializedcurrent seed, and whether it is ready

Calling any of them before the seed arrives throws InvalidOperationException. Check IsInitialized or wait for roomJoined.

The rules that keep it in sync

Determinism only holds if every client makes the same calls in the same order:

  • One generator per shared decision stream. If client A rolls twice for an effect and client B rolls once, they drift apart forever. Never use client.Random for things only some players do (local particle effects, UI jitter). Use UnityEngine.Random for those.
  • Same order everywhere. Roll inside code every client runs identically (for example in response to a networked event all clients receive), not inside per-player Update loops.
  • Late joiners start from the beginning. A player who joins mid-game gets the seed but not the sequence position. If rounds matter, store a round counter or a fresh seed in the room properties, and re-initialise at the start of each round: client.Random.Initialize(newSeed) after the master changes randomSeed (listen to OnRoomPropertiesChanged).
  • Seed must be an int. A non-integer value is ignored and the generator stays uninitialised.
  • Not for secrets or fairness against cheaters. Every client knows the seed, so a modified client can predict every roll. For anything with real stakes (gambling-style loot), let the master (or your backend) roll and announce the result.

Re-seeding each round

// master, at the start of a round
await client.SetRoomPropertiesAsync(new() { { "randomSeed", UnityEngine.Random.Range(1, int.MaxValue) } });

// everyone
client.OnRoomPropertiesChanged.AddListener(props => {
    if (props.TryGetValue("randomSeed", out var s)) client.Random.Initialize(Convert.ToInt32(s));
});

The SDK only auto-initialises on roomJoined; re-seeding mid-room is your code's job, as above.