Exploring randomness

Now one strange thing to keep in mind is that there is no such thing as a completely random system, especially in gaming and computing. You can get close with some really complex mathematical systems but nothing is truly random. The best we can do is make it random enough to fool the player making them believe it is random.

The reason for this is simple: computers are not random and don't think in random terms. When they generate a random number, they are using a seed (a unique number to base their random generation on) to work out what number to give you. But every time you generate a number based on that same seed, it will always be the same sequence; this is known as pseudo-random.

So, if I generate a random number from a seed of 1234, every number generated from that seed will always follow the same pattern (1, 5, 3, 7, 2, 4, 10, and so on). Most basic systems try to balance this out by also randomly generating the seed number, but this again falls under the same pattern. However, it does make the random pattern a little more random. A lot of systems use the date or current clock tick as the seed. It's important to know and remember this when you are planning to use random systems.

There is also a drawback to trying to make your random system even more random: you end up spending more time computing the random number in your game and stealing resources from other systems such as physics, AI, and so on. It's always a balancing game to ensure you plan where your precious system resources are going to be spent. For instance, if you use a triple-pass-random system using several levels of Perlin noise generated for each frame, it is a heavy burden on the CPU (although, this is a rather extreme example, you should never generate it for every frame unless there is a very good reason to do so!).

In most cases, developers use other effects to try to create randomness by using noise generating systems (Perlin/fractals/Gaussian drift) and other techniques to try to make the best use of low-cost generation systems with as few passes needed to get the desired result. By combining two or more systems, you can create an approximate and fairly complex random system.

Note

If you want to read up more on random and pseudo-random systems, you can get a full history on RANDOM.ORG at http://www.random.org/randomness/, which also features some examples of free and paid random systems.

There is another side to this predictability of basic random number generation systems: these can be used in various procedural techniques to build game items. If you can predict a sequence of numbers based on a particular seed, you can use that sequence to always build the same thing each and every time.

So, if you want a set of events to always occur in a particular order, you can actually use the basic random system to create a fixed event system; just use the seed you need to generate the sequence you need to use.

Planning for random code/generation

A key point of any good game design when you even start to think about adding random code/generation to your game is to stop/look and listen. Never rush into using random systems in your game, else you will end up rewriting it at least three times before you're done, and even then you won't be absolutely happy with the result.

Start working from a simple base and ask yourself:

  • Do I actually need it to be random or will it get configured?

    This is the first and most important question: are you trying to add random code/generation because it's easier to throw in, and will a fixed configuration be more suitable (is it really random you're after)?

    Never use randomization lightly, even when it is just a range of numbers you want to pick from; always question whether it is the right tool for the job. Inevitably, using randomization is always going to be more expensive in terms of processing (especially with more complex systems with deepening levels of recursion or noise generation) than a simple mathematical equation to approximate the values you are after. Do your research.

  • Where in your design do you see the need for randomization?

    Be specific! What do you actually need to be randomized or sampled?

    For example, in this RPG project for the random battle events on the map, we need to figure out the following:

    • The chance of an event occurring on a journey
    • Where on the journey the event will occur
    • What will be the starting condition of the battle, number of enemies, their strength, and who fights first
  • In each area, how frequently will you need a random sample?

    Because of the cost of random selection, you need to decide when and where the generation will take place. If you need a single random for each frame, that might be okay (depending on what else is happening in each frame); but if you have many, then it may be better to prefill an array of random numbers at the start of the scene and perform a predictive selection of numbers in that array (either stepping through or selection based on other factors).

  • What level of complexity does the random sampling/generation need?

    So once you've decided where you need randomization and how often you need it, only then do you decide on how complex that generation needs to be. Is it simply picking a random number or do you need a more accurate random number predication by using one of the aforementioned complex techniques such as Perlin noise or fractal sampling?

    This is arguably a much trickier question as how random do you need to be, in a lot of cases only testing will tell; does your current random technique let you down and the pattern always seems obvious? Does it hamper your gameplay?

For the purposes of this book, I will keep the use simple; this section is mainly to highlight all the complexities of using random systems in games. This might sound like a nice idea to begin with, but beware, here be dragons, even if it's just as simple as a single random number picked in a range in each frame.

Note

Another important consideration is that random generation is not free. Depending on the system you use it could also generate garbage and hamper the performance of your game that may not seem obvious at first glance.

True randomness

There is another course of logic in random generation systems called True Random Number Generators (TRNGs). They go to great lengths to guarantee the randomness of a generated number with greater and greater precision, but these also come at a heavy cost (if you really need them, however, they are worthy of study).

In games, however, it is usually sufficient to rely on pseudorandom systems, both for their efficiency as well as their predictability. They can be used for bug reproduction or being able to do lock-step games over the net with only player input, for example, in RTS games, or level-generation systems based on seeds, for example, Worms. Another reason is that you often don't want 100 percent randomness, you want something like a shuffle bag or similar to ensure that the event happens "randomly enough" within a time frame.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.19.174