- Home /
Generate the same 'random' number sequence from a seed
Hi, I'm trying to generate the same set of 'random' numbers, given an initial seed. I'm currently doing the following steps to try to get the same numbers generated each time.
- Set seed to be a const int by setting
const int seed = 123;
- Save the initial state of the number generator on Start() by saving
var state = Random.state;
- Set the initial state in my method by calling
Random.state = state;
- Specify the same seed each time by calling
Random.InitState(seed);
Later on I called both Random.Range()
& Random.value
and expect given the same saved state and same seed that the same values should be generated, but this is not the case.
Does anyone know where I'm going wrong? or any other ways of generating sudo-random number sequences with seeds?
Cheers
I've just re-read the following page: https://docs.unity3d.com/ScriptReference/Random-state.html
This makes sense to me, it just doesn't seem to work. It's like the state isn't being restored properly and so doesn't generate the same 'random' values.
Answer by jimmycrazyskills · Mar 28, 2021 at 07:09 PM
No worries, I found the issue. It turns out I was sometimes calling Random.Range() more times than expected, so it was pushing the sequence of 'random' numbers out from what was expected.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do you get a seed value from UnityEngine.Random.State? 2 Answers