- Home /
Int Value Not Randomizing
I'm trying to randomize and int -- grid.seed. But when I go into play mode, the seed changes, but to the same number depending on what it was originally. For example, if grid.seed = 1
, then this code changes it to 4010. If grid.seed = 2
, then it changes it to 2621. If grid.seed = 3
, then it changes it to 5858. This is really frustrating because I really need this variable to be random, and I don't know why it's reliant at all on what 'grid.seed' is originally. Can anyone help me out and tell me what's going on here?
namespace Grids2D {
public class RandomizeSeed : MonoBehaviour
{
public Grid2D grid;
private int seed;
void Start()
{
grid.seed = UnityEngine.Random.Range(1, 10000);
}
}
}
Thank you for your time.
Answer by logicandchaos · Jan 28, 2020 at 12:27 AM
I'm pretty sure this has to do with your seed value. Random number generation only creates psuedu random numbers. A basic way to explain is they made a huge look up table of dice rolls and your seed value is where you start on that table.. So whenever you start with the same seed value you get the same result, a lot of developers will use the system time as the seed value. There is an excellent GDC talk where one of the speakers goes on about a lot of alternatives and how you can use the pseudo random nature of the numbers as a mechanic, like a meta game.
Thanks a ton for the reply! Do you know of any way to randomize the seed?
Exactly the same way you randomised the number. Stochasticity is normally faked in computing ... to the point at which there are companies which generate higher entropy randomness by fil$$anonymous$$g walls of lavalamps to get over the problem.
Answer by Bunny83 · Jan 28, 2020 at 12:58 AM
Unity does initialize it's own PRNG with the current system time by default. Keep in mind that Unity only provides a single PRNG. So when you set the seed
So when you use Random.InitState somewhere in your code any Random.Range call after that will follow the same sequence if you provide the same seed each time.
We have no idea what your "Grid2D" class does and what your seed variable in that class actually does. We also don't know about the execution order of your scripts. If for example your Grid2D script also has a Start method which might manipulate Unity random number generator, the result highly depends on the order in which your scripts get executed. By default there is no specific order unless you set the script execution order for certain classes.
In sort: There is not enough information to solve your issue.
Yeah you're probably right. Grid2D is from the asset store, and I don't fully know how it works. Thanks.
Answer by Wittz8 · Jan 28, 2020 at 01:19 AM
I figured it out -- not sure why this works. but I added the line Random.InitState(System.DateTime.Now.Millisecond);
to the beginning of the void start()
function.
namespace Grids2D {
public class RandomizeSeed : MonoBehaviour
{
public Grid2D grid;
private int seed;
void Start()
{
Random.InitState(System.DateTime.Now.Millisecond);
grid.seed = UnityEngine.Random.Range(1, 10000); //randomizes the shape and location of the cells
}
}
}
Now it actually randomizes. Thanks for all the input!
Your answer
![](https://koobas.hobune.stream/wayback/20220612222228im_/https://answers.unity.com/themes/thub/images/avi.jpg)