- Home /
How to reset Random.InitState(x)
Random.InitState(42);
noiseValues = new float[5];
for (int i = 0; i < noiseValues.Length; i++)
{
noiseValues[i] = Random.value;
Debug.Log(noiseValues[i]);
}
}
console log: 0.988, 0.588, 0.436 , 0.343, 0.463
As is known, it always gives the same values. Can I create a different result with the same seed?(like Random.InitState(42) ) Can I reset the random's memory? Also, I could not grasp what exactly the seed mean. I would be glad if you could explain.
Answer by Bunny83 · Oct 09, 2020 at 05:20 PM
The point of calling Random.InitState with a seed is to reset the random number generator and to put it into one particular initial state based on the seed. When you call InitState with a particular seed, you get always the same sequence of numbers back.
Hopefully you know that you can not generate true random numbers with a computer unless you have special hardware that can generate random numbers based on some external source. All random number generators we use in software are pseudorandom number generators (PRNG). A certain PRNG always generates the same sequence / cycle of numbers. The seed essentially puts you somewhere in that sequence. Most PRNGs have huge sequances.
I'm not really sure what you're asking or what problem you want to solve. However passing the same seed to InitState will produce the same sequence, always. If you want a different sequence, use a different seed. To get seemingly random numbers the Random class is usually automatically seeded with the current date and time. Of course when you call InitState manually you reset the generator into the same one state based on your seed.
While, in most cases, it's true that a software usually uses a PRNG for generating random numbers, it's not true that it requires a special hardware to generate a true random number. There RNGCryptoServiceProvider class (in .NET) allows you to generate a true random number generator, but it's not as easy to setup as common PRNG -based classes.
Here's the link toward an example of a true random generator created with Windows' .NET: Link
No, that's not true at all. The RNGCryptoServiceProvider generates random numbers which are considered cryptographically secure. They are still just pseudo random numbers. In an ordinary .NET environment the algorithm behind that class is based on windows CryptGenRandom function. In an .NET Core environment it's based on the BCryptGenRandom function.
The reason why it is considered cryptographically secure is because it includes many different factors of the machine, the current process and the hardware to calculate a pseudo random number. This makes it much harder to know or predict the state of the generator as it's almost impossible to know or capture all those features at once at the same time. However it is still just a PRNG and has nothing to do with "true" random or to be "more" random which in itself doesn't make much sense.
I'm not sure if that class will use specialized hardware if available. If it doesn't at the moment it most likely will in the future. Though the fallback will always be a PRNG solution similar to the current one. As I said, computers can not generate random numbers or random events without an external source.
Some modern motherboards may have a crypto processor. However it's not that they can produce true random numbers but their exact implementation and internal state is hidden which provides the security through obscurity and make the actual crypto process more resilient to tampering.