User blog:Pgrobban/Computers and random numbers

We often see a lot of posts about how "lucky" someone was, whether it's getting the item that you wanted from a reward, the Lucky Draw, or upgrading S-grade treasures from +8 to +9 on your first try. We also often curse when the opposite happens: when you spent a million coins and that damn treasure still fails to upgrade.

When it comes to games such as Cookie Run, "random" events don't have much to do with actual "luck". The outcomes of these seemingly random events are actually predictable if you know a few things about how a computer generates "random" numbers. These numbers are then fed into code that determines what will happen based on those numbers. For example, if there's 10% chance that a treasure will evolve, we can generate a number between 1 and 100 and see if that number is 10 or below. If it is, the code will tell the game that the evolution succeeded, otherwise, it failed.

Computers normally can't produce random numbers naturally. Why? Because computers follow very specific instructions, and telling it to "generate a random number" isn't specific enough. Instead, most games rely on pseudorandom number generations (PRNG). Pseudorandom means that the numbers will appear to be random, but are actually following a pattern.

A common way to do this is to pick an initial number (called the seed), and then perform some advanced mathematical calculations to get results that are wildly varying. For people who are interested in code, here's an example implementation of a function in C that generates a pseudorandom number (don't worry if you don't understand it).

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767 {   next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768; }

void srand(unsigned int seed) {   next = seed; }

But the problem is that if you know the seed, you will always get the same random number "chain". We can just feed any number as the seed into the code and see what numbers we get back. Speedrunners, people who try to complete a game as fast as possible, use this fact to their advantage. For example, if the seed is determined by the frame number of the game, they runners can stall for exactly the amount of frames they need to get the item that they wanted or always getting critical hits against their bosses.

A better way to produce random numbers is to use the current date and time as a seed. Early game consoles didn't have internal clocks in them, so that's why they were easily manipulable. But even with a clock you can manipulate it by setting it to a value that produces a known result. We can assume that a game like Cookie Run also uses this method, so in theory, we can determine the outcome of the random events like opening chests or upgrading treasures. But it's very likely that the number generations for CR happen on the server side, which us players don't have access to, and means that it will be difficult for us players to manipulate (unless someone hacks into their servers and modifies their code :P)

So can computers ever achieve true random number generation? Well the answer is sorta yes. One way to do it is to hook up receivers that measure things like atmospheric pressure or voltage in an outlet with extremely precise insruments and then feed it to the code that generates the random number. The site random.org for example uses radio receivers to pick up atmospheric noise. But of course, adding these receivers to video game consoles, particularly early ones is infeasible and the hardware to process the data was too slow. So for most people, having a quick and dirty PRNG works fine.