How to get a different Random.seed value each time?

I’ve searched this forum and unfortunately, it seems that every post wants to know how to generate the same pattern every time. In my case, I do not want to generate the same pattern and wish for the game to create a random pattern each round.

In C++ I would have done this by seeding the randomizer with the current time in milliseconds. For Unity it seems that Time only can give you seconds since the start of the game, since last frame, etc.

Since my randomizer function is needed on start-up, the Time.realtimeSinceStartup value always equals zero.

So, is there any way to get a useful number for the seed that is different each game? If I wait 10 seconds, the seed will just be 10…

Try System.DateTime.Now.Ticks

You could also use any of the other properties of DateTime.Now, such as Second, Millisecond, Day, etc…

In that case you don’t want to touch Random.seed at all. The whole point of Random.seed is to generate the same pattern, so if you don’t want that then don’t use it.

–Eric

When I don’t use it I get the exact random sequence…How are you supposed to not get a repeating sequence in unity? In every game I have written (outside of unity,) I seed the clock with something that is different every launch and then call random however many times I want?

I’ll try the ticks method mentioned.

Thanks

1 Like

Works for me just calling Random without the seed.

I don’t and never have, and I doubt there’s anything special about my copy of Unity.

function Start () {
	Debug.Log (Random.Range(0, 100));
}

Results with repeated running (with no other code anywhere that touches Random.seed):

3, 77, 68, 99, 72, 46, etc.

The Random class already uses the time or something behind the scenes for the seed. If you set Random.seed, then this doesn’t happen.

–Eric

Bumping an old thread, I am setting the seed for my procedural generation. However, I have need to use a truly random value when awarding random content.

Random really needs a ‘new random seed’ option using the clock or whatever seed initialisation Unity currently uses.

Can’t you just call InitState whenever you want?
https://docs.unity3d.com/ScriptReference/Random.InitState.html

You can, but you need to give it a value. Currently I’m using System.DataTime.Now.Ticks. You could also record the random.state before initialising to a static seed and then restore it when you want to continue with random values. As the need to swap between randomised Random and seeded Random is pretty much a given if you ever need a seeded Random (seeded level generation consistent between creations; ‘unseeded’ random event resolution when that level is played), it should be an intrinsic method IMO.

Technically that’s not what an intrinsic method is, but I think I get what you mean.

You believe there should be a Random.InitStateWithCurrentTime?

But my wonder is why? All this saves you is writing this:

Random.InitStateWithCurrentTime();

vs

Random.InitState(System.DateTime.Now.Ticks);

And what if I want milliseconds? Or based on the time since startup? Should we also have:
Random.InitStateWithCurrentMilliseconds
Random.InitStateWithCurrentTimeSinceStartup
Random.InitStateWithTheNumberOfGrainsOfSandFoundInTampaBeachBySallyOnDecember12th2014
???

1 Like

If you’re wanting to restore ‘random’ numbers, you don’t care what method is used, so flexibility is useless. You just want Unity to go back to using ‘unseeded’ numbers. Saving people having to look up how to do that by having an obvious method to restore unseeded random is in every way a plus. It wouldn’t even be at all hard for Unity to implement as they just need to seed the random engine with whatever they do in the first place when the game is run.

The saving here is 50,000 developers all spending 15 minutes looking it up, versus 1 dev spending 15 minutes to include a ‘Random.InitStateUnseeded()’ method. An awful lot of instructions are convenience and helper functions and not necessary as they could be implemented writing more code…

Well all states are seeded… you can’t have a random sequence with out a start point (the seed). Unity just picks a seed for you… and it’s not the same every time. Every time you start the game the seed is different (like how using ticks/milliseconds will be different depending the time of day you seeded it).

What would this ‘Random.InitStateUnseeded’ do? Would it reset it to the state it was when THIS game started (so the seed it used on startup)… or is it creating a new seed based on the algorithm Unity generally uses (ticks/milliseconds/whatever it is they use?).

If it’s the former… just use your own consistent seed by initializing the state on start.

If it’s the latter… what does it matter what you use as the seed in that case?

Saving 50,000 developers all spending 15 minutes to look it up? They’d have to look up ‘InitStateUnseeded’ as well (and I’d have to look it up too since the name makes no sense to me… what is an unseeded state?)

Any developer would/should lookup the API they’re programming against… and documentation exists for the method they did make available. It’s InitState and it explains exactly how to use it:

1 Like

Sorry for being pedantic, but warning bells are going off from the “truly random number” bit. Truly random numbers are literally impossible to create in software development without an external noise source- it’s a very well-known limitation in programming. In hardcore cryptography, they have to do ridiculous things like parsing realtime visual data from steam/smoke or lava lamps to generate random seeds (as an example).

Unity’s Random class, and most RNG systems in existence that don’t have an external noise source, just use the time to generate the seed, and it’s theoretically possible to get the same seed with two clients, always, because true randomness is impossible at that level. That said, if you’re generating the seed from something like ticks/picoseconds in time measurements, the chance of getting the same value on more than one client is effectively zero. Unity already does this, it generates a new random seed on start based on the time, so if you want the best “random” you can get, you already have it.

The main reason to use your own seed is to be able to force the same sequence, not to somehow further randomize it (which you can’t). There is no “unseeded” mode.

At least, that’s my understanding of the situation. I’m sure someone will correct me if any of the details are off.

1 Like

No, you just have it as part of the Random.InitSeed() documentation. I’d personally have Random.InitSeed() without a parameter value reset it to a continuation of the default random state before giving a seed.

If you are assigning your own random seed, you look up the documentation on how to do that. If you then want to revert to Unity’s default random number series, there’s no information on that and you have to go looking and then suss out the system clock option etc. There’s no explanation on how Unity generates its random values by default. As a user, you use Random() to get random values and they are always random as you see them. Every time you run your project, they are different. The moment you add a seed, for the purposes of controlled procedural creation, you can no longer get back to that ‘random’ set of values, and there’s no documentation on how to return to that ‘random’ set of values.

Yes, truly random is not possible, which is why I always used quotes and the like around the word. By 'random; I mean creating a non-repeated set of values, unlike once you have provided a seed value. Use of the term ‘unseeded’ is just as wrong. There is no single defined term I know of to say, “default seeded value for creating a unique random series.” :wink:

I agree the documentation of Unity’s Random is very lacking.

Though I still argue this ‘InitStateUnseeded’ method isn’t the answer.

I’d argue the answer is Unity adds to the documentation of both Random and Random.InitState something like “The default initial state used by Unity is the time of day in milliseconds when the application begins” (or whatever their system is).