CodeBlanks Logo Icon CodeBlanks

Random seed

Learn repeatable randomness with the same seed.

Random Seed and Determinism

Randomness in computing is pseudo-random. Pseudo-random number generators initialize from a starting value called a Seed.

// The same seed always produces the identical sequence
Random.InitState(42);
int num1 = Random.Range(1, 100);

Using the identical seed causes the generator to yield the exact same sequence of pseudo-random numbers every time, a property known as determinism.

Seed Sharing and World Synchronization

In multiplayer titles, sending entire world datasets across the network is unnecessary; transmitting a single seed integer (e.g. 94821) suffices.

public void SetWorldSeed(int seed) {
    Random.InitState(seed);
}

Both clients run the procedural generator locally with that shared seed, producing identical map topology.

Practice Deterministic generation with seed

Define a Start method. Inside, declare int seed = 42; and print "Kullanilan tohum: " + seed to the console using Debug.Log.

Starter code

Player.cs
using UnityEngine;

public class SeedTester : MonoBehaviour
{
    // Start icinde seed tanimla ve yazdir
}

Sample solution

Player.cs
using UnityEngine;

public class SeedTester : MonoBehaviour
{
    void Start()
    {
        int seed = 42;
        Debug.Log("Kullanilan tohum: " + seed);
    }
}

Expected output

Kullanilan tohum: 42

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What occurs in Unity when Random.InitState(seed) is called with the identical seed value?

  • The game immediately crashes and closes
  • Character speed doubles every frame
  • All game save files are deleted
  • The random number generator produces the exact same sequence of numbers each time Correct answer

Explanation: The same seed resets the pseudo-random generator state, ensuring subsequent outputs occur in the exact same sequence.

2 Initialize random state

Question
Random.____(seedValue);
Answer
Random.InitState(seedValue);

Explanation: The 'Random.InitState' method is used in Unity to initialize the random generator with a specific seed.

3 Randomness and seed concepts

Term Meaning
Seed Seed value starting the generator
Random.InitState Unity method configuring the seed value
Deterministik Same input always gives same output
Pseudo-random Fake randomness made by algorithms

All lessons in Advanced