ELi5 , how do random number generators work?

57 views
0

How is it different than the program just choosing the number it wants within range?

In: 6

Simples RNG works as follows:

1. take a number between 0 and 1, with lots of decimals.

2. Multiply by a really large number

3. Keep the decimals, drop the integer part (e.g. 123.4567 becomes 0.4567)

4. You have another number between 0 and 1, and there is no easy way to predict its range if number in step 2 was indeed large.

5. if you want more random numbers, take the number from step 4 and use it to repeat steps 2-3.

NUmber from step 1 is the “seed”. Same seed means same sequence of “random” numbers. Which is useful for debugging. For a truly random choice of seed, take system clock and use it decimals from seconds counter.

Computers have a very hard time generating actual random information. To generate the entropy (chaos) needed to get actual random information, it is often necessary to use an external source of random data.

Short of that, computers can use something called a pseudorandom number, which is not truly random- but is random enough for many purposes, or within a certain context. For example, using the number of microseconds elapsed since a specific time together with information about how a user moves a mouse is one way.

Truly random data is frequently created by using things like weather patterns, radioactive decay, or- my favorite- pointing a camera at a wall covered with lava lamps and putting the resulting picture through an algorithm to create random numbers.

Typically by using a pseudo-random number from a seed. So imagine you start with a seed something like the current second-minute-hour-day-month-year so a number like 20220708155533. They then do a series or predetermined math calculations (like say x73, -1234567890, x123, +9874561230, x258369, -7777777777) then take the digit that is in the 10000 digit as the random number.

There are different kinds of random number generators, but most modern ones sort of work on similar principles. Note that, to be pedantic, if we’re talking about computer random number generators they are usually “Psuedorandom Number Generators”. This is because they all have characteristics that make it possible to predict their sequence if you have enough information. More on this at the end.

In older, more limited games, the game was usually updating itself every frame in a loop. It might keep a “frame counter” that is what it says: it starts at 0, is set to 1 on the first frame, then 2, then 3, etc. Some random number generators would base their random number on that frame counter. For most players this was fine, but for speedrunning types this is exploitable!

The more complicated ones revolve around some mathematical function. This function takes some numbers as inputs and spits out other numbers. If you feed the numbers you get back into it, you get different numbers. If you do this long enough, you’ll see the entire sequence repeat. If you start with different numbers, you’ll start somewhere in the ‘middle’ of that sequence. It will repeat after the same number of tries, but the sequence will look different since you “started” in a different place. Logically speaking, it’s like all of the numbers are in a table and the numbers you start with determine where you start. After that, you keep getting the “next” number in the table until you reach the end and start over.

To pick the “start” numbers, a lot of programs use the computer’s current time. That guarantees if you start the same program at different times you’ll see “random” events happen in a different order. But even *that* is exploitable by very determined people, so the random number generators for “important” things like online casinos use more complicated techniques to pick their starting numbers, like looking in random parts of computer memory and using the values there. Some encryption programs ask you to move the mouse and type for a while before they start work: they’re using your keystrokes and mouse movements to pick the starting numbers in a way that is less predictable than just using the time.

Even then, if enough numbers are generated the sequence repeats. So really fancy cases like online casinos will periodically “reseed” their generators by picking new “start” numbers. This is not unlike shuffling decks more frequently to try and thwart card counters.

There are also some “true random number generators” that are completely not predictable but they are too complex and expensive for most people. They rely on real-world phenomena like “When does this sample of a radioactive material emit some energy?” to pick numbers. There’s even an interesting project that pointed a camera at a lava lamp for a source of true randomness! Since these tend to involve cameras or other fancy sensors, they aren’t used much outside of laboratories.

There’s basically two ways to do it.

The first method is called “pseudorandom generation”; and uses (usually) unpredictable math to generate random numbers. There’s a few ways to do this; but basically all of them involve taking a starting number, doing some math to it to give a new number, and taking just a part of the new number. For example, I take a 5-digit number, square it, drop the ones digit, and reduce it back to 5 digits; then return the ten thousands digit of the new number. If I need a 2-digit number, I do the process twice: once for the ones digit, once for the tens.

Doing this is very hard to predict – especially if sometimes I make it harder by sometimes throwing away random numbers; or by re-seeding with a function of the current time (likely using fractions of a second). But it is possible to predict – you can’t use this kind of random number generation for cryptography; and it’s usually illegal to use this kind of random number generation for online gambling.

​

Which brings us to “entropy generation” or “hardware generation”. This method uses some outside source of randomness: for easy randomness, things like the delay (measured in milliseconds, often only using the ones digit of milliseconds) between computer inputs can be used; but for “true” randomness, there are several companies which do things like film lava lamps, measure small differences in atmospheric conditions, or use radioactive decay – all of which are as random as you can get – to generate random numbers.