17 May 2015

Quick Thoughts on RNG - Part I

My desktop has been out of commission (or at least making unusual noises that spawn distrust in its use), so I've wanted to tackle some smaller logical problems on my laptop while I await replacement parts. One of my serious side projects involves a portion with randomly-generated content. It's something I haven't actually gotten around to yet more out of RNG reluctance than lack of time.

The Thought on My Mind


"Does the default "Random" behavior of a language such as C# give enough random for use in a game engine?"

Honestly, this is more ingrained as a "problem" from looking into the concept of random, and how we can only simulate random. My first assessment is really just to see how "Random" the default C# RNG is.


Quick Sample


So I created a program to enter a max number (so 6 would give a random number between 1-6) and the number of times to roll a random number. If I just output the results, they don't always look that promising:

Max number: 10. Rolled 100 times.

Look at that cluster of 10s! If I were actually rolling a d10, that would seem pretty unusual.


On the Other Hand...


Now if I output both the Individual Results and Distribution of those results, it looks a bit different:

Much better when you can see Distribution.

Ignore the 6 milliseconds taken -- I was curious to see if it would take any time to generate more random numbers, but it seems the only reason it adds time is outputting the results.


In Closing


I suppose it's difficult to tell on a quick pass whether the default RNG is useful. Depending on what, specifically, you're using it for, then sure. The distribution in the long run is actually not terrible. It's just in a short window of time where it doesn't look so good.

For practical purposes, you'll probably be using a RNG with weighted results rather than a straight 1-to-1 distribution (if you're calculating loot in a game, for instance, then a "Common" drop would ideally count for 50%+ of the total possible random numbers rolled). That could have more of an impact on results, but that will also take more thoughts and testing than this quick run-through.