Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseGets or sets the full internal state of the random number generator.
This property can be used to save and restore a previously saved state of the random number generator. Note that state
is serializable, so that determinism can be preserved across sessions. Determinism is an important trait in many scenarios, such as multiplayer games, reproducible simulations, and unit testing.
Generator state can be (re)-initialized in two ways:
See the following example for an explanation of how these work.
using UnityEngine;
public class ExampleClass : MonoBehaviour { void Start() { const int initialSeed = 1234;
Random.InitState(initialSeed); // cannot be retrieved
PrintRandom("Step 1"); PrintRandom("Step 2");
Random.State stateBeforeStep3 = Random.state; // can be serialized
PrintRandom("Step 3"); PrintRandom("Step 4");
Random.state = stateBeforeStep3;
PrintRandom("Step 5"); PrintRandom("Step 6");
Random.InitState(initialSeed);
PrintRandom("Step 7"); PrintRandom("Step 8"); }
static void PrintRandom(string label) => Debug.Log($"{label} - RandomValue {Random.Range(0, 100)}"); }
/* Output:
Step 1 - RandomValue 38 Step 2 - RandomValue 76 Step 3 - RandomValue 69 Step 4 - RandomValue 11 Step 5 - RandomValue 69 Step 6 - RandomValue 11 Step 7 - RandomValue 38 Step 8 - RandomValue 76 */
The values from step 5 and 6 will be equal to those from step 3 and 4 because the internal state of the generator was restored to what we saved in stateBeforeStep3
. Also, the values from step 7 and 8 will be equal to those from step 1 and 2 because we are resetting the generator state with initialSeed
via InitState, which leaves the generator in the exact same state as right before step 1.