### Computers are deterministic<sup>1</sup> > In mathematics, computer science and physics, a deterministic system is a system in which **no randomness is involved** in the development of future states of the system. A deterministic model will thus **always produce the same output** from a given starting condition or initial state. > <sub>[Deterministic Systems][1] - Wikipedia</sub> Think of a computer like a cook, who has been handed a recipe (a program) to bake a cake. Computers **do not deviate** from the instructions (input) in a program, meaning that each cake they bake (output) will be identical. --- ### So how do programs (including games) implement randomness? Kinda like this: [![xkcd 221: Random Number][2]][2] <sub>[Comic #221: Random Number][3] - xkcd</sub> A mathematical formula is used to "fake" randomness. This is known as a **Pseudorandom Number Generator** (PRNG), often abbreviated to just Random Number Generator (RNG). The problem is, being a mathematical formula - if you give it the same input value, you'll get the same output value. So we're left with a problem: ### What is the initial starting value? This is where the "seed" gets its name. It's a **seed value**, a number used to start the random number generator, so that it may generate future pseudo-random numbers. In most games, the seed is determined automatically using a few tricks, like deriving a value from the system clock, or the number of frames generated before user input. But this is also why clock manipulation or frame-counting are popular tactics in speedrunning circles - because they can take the "randomness" out of certain actions, to guarantee a more perfect run. In other games like Minecraft which relies heavily on randomness for it's world generation, they allow you to manually specify a seed to get the "same" world generation. --- ### Math/Comp-Sci crash course: Just for fun, let's look at a working example of a basic pseudorandom number generator<sup>2</sup>: ``` Next_Value = (Multiplier * Current_Value + Increment) % Modulus ``` The Modulo `%` operator gives us the remainder after division by the Modulus. For example, if `10 / 8 = 1 r2`, then `10 % 8 = 2`. So let's set some values<sup>3</sup> in the formula, and generate numbers between 0 and 100: ``` Next_Value = (7 * Current_Value + 5) % 100 ``` We need to seed `Current_Value` with something, so let's do a seed of **1**: | Current Value | Formula | Next Value | |---------------|--------------------|------------| | 1 (Seed) | (7 * 1 + 5) % 101 | 12 | | 12 | (7 * 12 + 5) % 101 | 89 | | 89 | (7 * 89 + 5) % 101 | 22 | | 22 | (7 * 22 + 5) % 101 | 58 | and now, let's try a seed of **42** | Current Value | Formula | Next Value | |---------------|--------------------|------------| | 42 (Seed) | (7 * 42 + 5) % 101 | 97 | | 97 | (7 * 97 + 5) % 101 | 78 | | 78 | (7 * 78 + 5) % 101 | 46 | | 93 | (7 * 93 + 5) % 101 | 24 | As you can see, we're getting some "fake" random numbers, based off a seed value! Pretty cool, right? --- <sub> 1. This is true for most computing applications you'll encounter day-to-day. In specialised cases (such as high-security applications) computers can act as non-deterministic systems by utilizing True Random Number Generators (TRNGs). These devices measure unpredictable physical phenomena (such as thermal noise or radioactive decay) to generate data that is truly random and not based on a formula. Non-deterministic behaviour may also be observed from situations such as multi-core processing (race conditions), hardware faults, or Single-Event Upsets (SEUs) (i.e. cosmic rays flipping bits, etc)<br/> 2. This type of PRNG is known as a Linear Congruential Generator (LCG). There are other (more robust) PRNG formulas, this one just serves as a good simple example. This LCG will loop after about 100 iterations, and visit almost every number in the range 0-100. Interestingly, it's impossible to generate 16, which is the "fixed point" for this specific equation (if you use 16 as the seed, you get 16 back: (7*16+5 = 117 -> 117%101 = 16) </sub> [1]: https://en.wikipedia.org/wiki/Deterministic_system [2]: https://i.sstatic.net/0bhcfStC.png [3]: https://xkcd.com/221/