Constant random number generator?

I am finding that I use so much RNG in my game that I really ought not to be generating all this randomness all the time. Is it a good idea to have one RNG script running, maybe with a co routine to have it generate once per second, and then pull that value for all my RNG needs by doing a

GameObject.Find("randomGenerator").GetComponent<rngScript>().randomNumber

Or is RNG not super processor intensive?

An example of how much RNG I’m doing, is at any point, an asteroid could be colliding with another asteroid, finding a random number of smaller asteroids to spawn, finding a random direction for them to travel in after spawning, loading a random explosion sound, all the while checking if enough weapon pickups are spawned and spawning a new random weapon, in a random position. That’s not even half of it.’

Should I have one RNG machine running at all times?

wait, why are you generating the numbers every second?

You generate them as you need them.

The point of a seeded random number generator is that every time you start it with the same seed you get the same series of random numbers. This way if you propagate something based on the same sequence of calls/procedures, you can get the same result of random distribution each time.

Think like how in Minecraft you have a seed for a map, and if you type in that seed again, you get the same map.

But if you’re calling it every second, there’s no guarantee that the sequence will line up correctly, because now the sequence is dependent on the clock, which can waver from machine to machine, day to day.

2 Likes

Trust me. Calls to Random.Range are not your bottle neck. What does the profiler say?

1 Like

Seconding that. Doing a static call to the built in random generator is not a slow thing. This, on the other hand:

GameObject.Find("randomGenerator").GetComponent<rngScript>().randomNumber

would be the slowest thing. The difference between that and Random.Range is that you’re searching through the entire hierarchy for a named object, and then getting the random number. The unity docs even says that:

Finally, if your game is running fine, don’t try to optimize it.

2 Likes