- Home /
Generating a good random seed
I made a game where the map is a set of corridors generated randomly using a lot of Random.Ranges, I got no problems until I got to making the multiplayer, I needed a Seed generated by the Host server and then passed to the other player connected (it's a Co-Op game, no more than 2 Players), I succesfully created a Script that resets the seed any time I press the button to reset the Map. However, my map is not random anymore, Unity generates only 3 or 4 random different sequencies and then starts repeating them each time I restart the Map;
Now my question, Can I write a Script which creates a totally random seed that generates always different sequencies of corridors? Or do I need to write my own random seed generator? If I need to, what's the code to make it work?
Answer by PlasmaByte · Dec 29, 2013 at 12:03 AM
One simple way to solve this is to use the computers time as a seed. That way it's pretty much guaranteed to be different ever time.
Perhaps use:
Random.seed = (int)System.DateTime.Now.Ticks;
Another slightly better option I$$anonymous$$O:
Random.seed = System.Environment.TickCount;
Both of the above options generate a seed that is sequential (e.g. 1, 2, 3, ...) which may be O$$anonymous$$ (depending on what you are doing) but if you want a seed that is really random then try this ...
seed = Random.Range(int.$$anonymous$$inValue, int.$$anonymous$$axValue);
... which will give you a random non sequential integer somewhere between -2,147,483,648 and 2,147,483,647.
I don't think this will fix OP's issue. Seeding with a random number generated from a pre-seeded Random object will result in even more deter$$anonymous$$istic behavior. The right answer is to use local time, as both @PlasmaByte and @ArnoC suggested.
You're fundamentally not understanding seeding. You're generating a seed from a none seeded random number generator :) All you've done is move the problem one level down.
Actually Unity's random number generator is already seeded with the current time when you start your game. So if the host want to generate a random seed that should be send to all clients you can simply use Random.Range to generate a seed.
Answer by ohokke · Aug 23, 2016 at 05:28 AM
The answer by @Aswissrole still gave me non-unique seeds when generating many seeds in a loop. My solution was to use the states of the Unity Random class, to have a single "state" or "generator" for only generating seeds, while not affecting other generator states:
private static Random.State seedGenerator;
private static int seedGeneratorSeed = 1337;
private static bool seedGeneratorInitialized = false;
public static int GenerateSeed()
{
// remember old seed
var temp = Random.state;
// initialize generator state if needed
if (!seedGeneratorInitialized)
{
Random.InitState(seedGeneratorSeed);
seedGenerator = Random.state;
seedGeneratorInitialized = true;
}
// set our generator state to the seed generator
Random.state = seedGenerator;
// generate our new seed
var generatedSeed = Random.Range(int.MinValue, int.MaxValue);
// remember the new generator state
seedGenerator = Random.state;
// set the original state back so that normal random generation can continue where it left off
Random.state = temp;
return generatedSeed;
}
This pretty much guaranteed to generate only unique seeds. Of course, ignoring the infinitesimally small chance a clash is generated. To solve this I'd say keep track of a list and keep generating until a unique number is generated, but in my case that chance is small enough to not need such a list.
That was exactly the point of this answer. He used a fix constant seed "1337" to generate other seeds for different sub systems. They always get the exact same seed every time. That's the point of specifying a seed manually. If you just want a random seed you don't have to do anything since, as we have discussed above, Unity already initializes the PRNG with the current date / time. So every time you start your game you will get a different seed and a different sequence of pseudo random numbers.
There are no good or bad seeds in terms of random numbers. Every seed is just as good as any other seed. The question was about how to initially generate a random seed which should be shared by all players. You could simply use the current time on the host as seed without any issues. Even "restarting" the game shouldn't be an issue.