Random Number Generation Part One

With all of this focus on procedural generation, I completely forgot to do a post on one of the most important building blocks for procedural generation.

Random number generation.  

Now, that being said, procedural generation does not directly require random number generation.  In fact, truly “random” numbers wouldn’t be entirely useful because it’s not reproducible.  Imagine a universe full of planets, each planet covered in generated terrain.  That terrain is covered by generated landmarks (cities / villages / forests / etc).  All of this data “can” be generated procedurally, but if you used truly random values for your generation you would never be able to visit the same place twice.  Each time the data was generated, the truly random values would be different and the same planet would have entirely different features the next time it’s loaded into memory.

This is where pseudo-random number generation comes in.  Pseudo-random number generators produce numbers that are seemingly random, based off of a starting seed number (or seed data in some of the more complicated algorithms) and always produce the same sequence of numbers given the same seed data.  Awesome, that produces random values but is entirely reproducible so it solves the problem above.  It also allows for much easier debugging as well, because you can do all of your testing with one specific seed and your “random numbers” will always follow the exact same pattern on every execution.

Most random number generators you might have used in your code, actually operate this way.  The rand() function in C/C++ specifically works this way.  You seed it using srand() and every subsequent call to rand() will use this data and previous values to generate new values.  The function itself is good enough to use for generating content (especially for games), but unfortunately it uses global data.  This will make your code rather ugly if you start using the random function to generate multiple worlds, add jitter to AI decisions, etc.

Designing a function to produce pseudo random numbers is a fun exercise to try on your own.  If you haven’t done it before, I highly suggest you stop reading here and go give it a shot.  Think about what properties you think are important in these functions, and think of a way to test and prove that it’s working the way you expect.

I would just like to clarify something before we move on though:  I don’t recommend creating your own algorithm because I think it will be better than the most widely accepted methods.  I think it’s an interesting thought experiment and will help your understanding of how a lot of these things work under the hood.  If your solution works well enough for your requirements, then by all means use it.

None of my articles are intended to advocate “re-inventing the wheel”.  I just believe that it’s healthy for developers to ask tougher questions, and dig deeper when it comes to how things work.

In the next part I’ll go into detail about the current random number generator I’m using.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>