# Strange behaviour when setting Random.seed

Hello,
in the code below 20 random numbers are first generated. After generating 10th number the seed is saved and rest of the numbers are generated as usual. After that the saved seed is set to Random.seed and another set of 10 random numbers are generated. If the line “Random.seed = seed” is not used the results are different than the last 10 numbers. Can you explain why this is happening?

``````    Random.seed = 1234;
int seed = 0;

Debug.Log("First set of 20 random numbers, after 10th number save the seed.");
for (int i = 0; i < 20; i++)
{
int r = Random.Range(0, 50);

Debug.Log((i == 10 ? " -> " : "") + r + " - " + Random.seed);
if (i == 10)
{
seed = Random.seed;
Random.seed = seed; // If this line is not used code gives different numbers in the last for-loop.
}
}

Random.seed = seed;

Debug.Log("Another set of 10 random numbers. Seed: "+Random.seed);
for (int i = 0; i < 10; i++)
{
int r = Random.Range(0, 50);

Debug.Log(r + " - " + Random.seed);

}
``````

The behaviour is correct.
If you don’t use line 14, then both the second half of the first for loop and the last
for loop have different seeds and this is why you will get different set of numbers.

But when you use line 14, then they both get the same seed value and so the
set of numbers are same.

I might be confused, but isn’t Random.Range using the seed currently in the Random.seed? To me I should get the same exact behaviour with or without the line 14. What is the use of reading Random.seed if it changes every time I use Random.Range and it doesn’t mean I can use the current seed to come back to that position in the random number series? It confuses me why I can get two different numbers with the same seed (when not using line 14).

1 Like

aah ok I got the problem now…Indeed its strange. If you seed the sequence with a
particular value(the value you store at i == 10), then it should give the same
numbers without line 14 as well…but before I point finger at Unity, I’m going to do a few more tests because this is really interesting