So, I’ve been working on the random number generator for my game engine. Over time a number of features crept into the RNG, and it started to grow weird limbs and such. In short, it was ugly to work on, and kind of a pain to use. Tonight I prettied it up and also made its features less gross.
There are two RNGs. One, RNG_Fast, is super basic and really fast, but its results could be randomer. The other is called simply RNG, and uses the Mersenne twister for randomerest results. It is a bit slower, though. Under normal circumstances, I use RNG, because the speed difference is usually negligible, and I prefer my random numbers to be as random as possible. RNG_Fast is actually only used in one place in any of my games right now (streaming world generation in Galaxy Chomp). It is useful when you need to do a LOT of RNGing constantly in-game.
Both RNGs have two main functions. One function just returns a random number between a given minimum and maximum. The other does this same thing, but weights the result toward a given target number. You also specify how strongly the result should be weighted.
Once I was finished fixing things and cleaning up the code, I thought it would be helpful to run some tests comparing all of this. After playing about with tests for a bit, I decided to have it spit out some handy graphs. Without further ado, here are the graphs:
For each graph, a random number between 0 and 99 was chosen 1,000,000 times. The X axis represents the numbers 0-99. The Y axis represents number of results. For the weighted graphs, 49 was used as the target.
RNG (unweighted)
RNG_Fast (unweighted)
RNG (weighted weakly, normally, strongly)
RNG_Fast (weighted weakly, normally, strongly)
As you can see, the differences in results between RNG and RNG_Fast are really quite minimal. My homemade RNG competes pretty well with the Mersenne twister based RNG. In fact, the only place I can see a real difference between RNG and RNG_Fast is the results for normally weighted RNGs.
In almost every test, RNG_Fast is roughly 30-40% faster than RNG. Usually this is going to be an extremely small difference in time spent RNGing, but for Galaxy Chomp the extra speed has proven essential.
I’ll try to get a post up soon explaining what’s going on at Cheese and Bacon Games. I started working towards a BBA in Accounting this fall, so work has slowed a bit, but progress is still being made.
EDIT: Oh dear, due to an unforeseeable error on my part, the normally weighted results for RNG_Fast are a bit off (“normal” weight meant 9 to RNG_Fast and 6 to RNG). So those two graphs should probably be about equal to one another. I suppose that is good news really, as it means my RNG has very similar results to the Mersenne twister one, but it gets them faster.