- Home /
How to make random number generation more random?
I'm creating a game that utilizes a lot of random number generation to create environments, generate loot, etc. When generating levels, my engine selects pre-created "chunks" from a set of 50+. However, I've noticed that a typical level will often have several repeated chunks, despite the level size being quite small. The same goes for loot generation-- a chest will frequently have multiple of the same item in it, despite this being very mathematically improbable if the numbers being generated were truly random.
I could change my algorithms for generating loot and levels, but I'm interested in figuring out what's going on with the RNG itself. It seems like random numbers that are generated in rapid succession have a good chance of being the same as one another-- or something like that.
I realize that true randomness isn't really possible, but I'm curious if there are any decent or best-practice ways to "enhance" the random number generation?
Unity's built-in random number generator uses $$anonymous$$arsaglia's Xorshift 128 algorithm, which is high-quality and does not have any time-based effects. This can be easily proven by setting Random.seed, then generating some random numbers. The sequence is always 100% the same regardless of how much time passes between each call.
What you want is something that's actually less random, not more.
Answer by nesis · May 29, 2015 at 12:30 AM
You're wanting a more robust way of calculating random numbers, to ensure an even spread. This kind of logic is used in roguelike loot systems to ensure that sampling probability distribution over some short timeframe ends up closely matching the desired distribution.
There are a number of ways to achieve what you're wanting. One way is to generate all the numbers you know you'll use in advance, adding them to a queue, then popping them from the queue one by one.
When doing this, you can add a check that re-rolls the random number if it's already in the queue too frequently, adding it only if it passes the frequency test.
If you have different "tiers" of things selected by the random number generator (eg, using the roguelike example, common items vs rare vs legendary), you can cater for an even spread of those in your queue by making A, B, and C number of them ready for the queue, then shuffling the queue.
Answer by tanoshimi · May 28, 2015 at 09:55 PM
How many chunks is your level made of? If it's any more than 10, say, and you're only picking from a set of 50, then the chances are more than likely that you'll have the same chunk twice. Randomness != Uniqueness. And, assuming that each loot is chosen with equal independent probability, the outcome of having two of the same loot is mathematically exactly as probable as any other outcome.
All computers can only ever generate pseudo-random numbers, it's true, but they're more than sufficient for any gaming applications. If you believe that your code is less random than it ought to be then please post your code - you might have a faulty implementation.
In the case of the chunks, not only are they the same, but they are also often rotated the same direction-- which there is supposed to be a 1 in 4 chance of. Also, the chunks tend to be bunched together-- meaning they were generated in rapid succession of one another.
Using debug.logs to print out the numbers, I can tell it's not a case of faulty implementation-- it's just the same numbers being generated more often than I'd expect. It's basically just using a loop with random.range to generate these numbers.
I know that RNG can take things like the system clock into account when generating numbers. $$anonymous$$y guess is that if I did something like have the computer wait a tick between generating different things, that might produce better results-- but I'd hope there's a better solution out there.
Again, I'm not saying that the things I'm seeing are completely mathematically impossible, it's just that this sort of thing happens on a frequent enough basis that it makes me think random generation isn't doing its job as well as I'd like.
"$$anonymous$$y guess is that if I did something like have the computer wait a tick between generating different things, that might produce better results"...this is incorrect; there are no time-based effects with Unity's random number generator.
Your answer
Follow this Question
Related Questions
Randomize values without exceeding a value? 3 Answers
Random.Range is not changing? 1 Answer
How to know what random number is chosen 2 Answers
[Beginner] Using Random.Range in initial game object position 1 Answer
How can I do random choices 1 Answer