# 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?

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.

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.