How do random number generators work?

In: 119

For random numbers you get a starting number, called a seed, and then perform a set of maths and bit manipulations to convert it to a new number. then that number is used as the seed for the next number and so on.

the kinds of manipulations done are swapping bits, inverting bits, multiplying by large prime numbers and taking the modulus. all things that make the resulting number seem unrelated to the previous.

a more modern approach is to take a hashing function, which is a function that takes an input and does similar manipulations to get an output, but with cryptographic security. this is slower, of course.

this means that given a seed, the same set of numbers are always produced. this can be useful if, for example you want to replay a computer game run to see if it was actually possible, or share some “random” artifact, or reproduce a set of tests.

sometimes you don’t want to be able to recreate the number ever, for example when making cryptographic keys or passwords. for these cryptographic random numbers you will definitely use a hash function in your generator and also get your seed carefully selected from the environment. things that get used for seeds like this: precise timings of things, network packets, keypresses, mouse movements, disk access. better are things that read the environment like radio receivers tuned to static. one system includes a high def video of a wall of lava lamps as it’s input to the random number generator.

For pseudo-random number generators, take a process that generates a chaotic, but deterministic string of numbers. Index into it at a somewhat random location (called a Seed) and use that.

For example, Pi is chaotic (non-repeating but deterministic). If you are looking for random numbers between 0-9, you can take a ‘seed’ which is just the number of decimal places to go to, and start reading decimals. So this algorithm with seed 542 would be: 9 0 7 0 2 1 7 9 8 and so on.

This is deterministic, anyone with the same algorithm and seed would get the same numbers. But unless you know what to look for, it will appear to be random.

Non-deterministic random number generators will use a process that is not repeatable. The common way this is done is to use sensor noise. For instance, take a microphone, and measure something. Like if a microphone is sensitive to xx.yy, measure to xx.yyzzzzzzzzzzzzzAA. AA is likely to be raw noise and totally random, for the purposes of a generator. Other common systems include cameras pointed at lava lamps (really!) or looking at the higher order bits from images. While theoretically if you set up the exact same physical experiment (with the sensors in the exact same state of being/wear) it would be deterministic, practically, due to physical process, those trailing bits are essentially random.

There are other ways to do it too of course, but those are the common ones.

As a specific example of how a random number generator might generate a single digit, it would use a *modulo* function. It’s essentially the same thing like what you probably learned in elementary school when a number is not perfectly divisible by another, and you have a “remainder”. Like, 102/5 is 20.4, or you could say 20 with 2 remaining (because 2 is not divisible by 5 without decimals). You would say this is as 102 *mod* 5 = 2.

So, grab a number that the computer has easy access to like the current time in milliseconds: 1652012941065. Grab another number like, I dunno, the CPU core temperature: 48. Take the last digit of that, which is 8, and do the first number *mod* the second number: 1652012941065 mod 8, which a calculator tells me is 1.

The odds of two number generators having the same core temperature are pretty decent. The odds of two number generators performing the check at the exact same same time are not *great* but not impossible. The odds of both happening at the same time are slim enough that it probably won’t happen much.

Of course, for security purposes, you’d want a much larger, longer number. So the process can be more complicated like multiplying the current millisecond time by a CPU clock cycle and then again by some other seed number and do *mod* of another big number made similarly.

How is a RNG used in a slot machine?

Depends on the generator.

Most computer generators use the date and time down to the smallest fraction of the second. It’s then put through a few complicated math formulas that basically says look at X digit and Y digit and do things with those. Ensuring every single time you rerun it you get a different number.

A great example of this is the Nintendo GameCube. The “clock” wasn’t based on the exact time of day but when the system was powered up. So it gave the same exact numbers, while technically random, in order every aingle time it was rebooted allowing you to manipulate “random” factors.

Most other machines use a more exact time of day or using memory saves a number to add to the formula to ensure it’s random. More modern consoles do this, even if you try to set the date of a Nintendo Switch, the string of numbers produced will be different.

great explanations; i watched a talk on 8 bit gaming which pointed out a couple of older methods

1. write an algorithm, like many have explained

2. use the current frame. variations and limitations in computing meant that the same part could have more or less.

3. Read from a predetermined list of numbers. Sounds crazy, but this was used in Final Fantasy 4 (i think) and nobody played it realizing this.

Random number generators don’t generate “truly random” numbers since computers are not able to just make up numbers. They make use of real-world processes (such as the milli- or nanoseconds of the current time, microphone background noise, the current speed or voltage of parts of the hardware, the last time some specific internal file was accessed, combinations of these and anything else that looks unpredictable), that look just random enough for people or programs that use random number generators to work with them. Mathematically speaking, we want good random number generators to fulfill two conditions:

(i) all numbers should have the same chance of being generated whenever the random number generator is used. We want equal probabilities because from a mathematical view, these are very easy to handle and if we ever want certain numbers to have varying probabilities (let’s say, for example, we want the numbers 1 to 10 to have a 5% probability of being generated each, and 11 to have a 50% probability), there are simple ways to build another generator with these probabilities from the first one

(ii) the random number generator is totally unpredictable, that is, no amount of knowledge we obtain should allow us to predict the generated number or even make assumptions about the probability of each number (besides the “all numbers have roughly the same probability” thing from above). In other words, nothing we learn about the random number generator should allow us to say that because of this knowledge, some numbers are now more or less likely to be generated than they would be without.

Technically, no human-made random number generator in this world fully satisfies either of these conditions: since all generators can do is take numbers from “somewhere” in the real world, throw some math at them and give us the results as our desired random number, there are lots of ways in which this process can become flawed and cause one number to become more or less likely than the others (think of a very simple generator that takes the nanoseconds of the current time and returns it as a random number between 0 and 999. There’s no way to guarantee the internal clock isn’t flawed and skips each 999, for example, making 999 impossible to be generated). Realistically, all we can do here is design the generating process really carefully and run tests (i.e. generate a lot of numbers with it and check if each number occurs about as often as the others). Then, in theory, every generator is not just not unpredictable but fully predictable: if we know everything the generator takes into account and the formulas it uses, we’d be able to predict the exact output of our generator. We choose to ignore this problem, however, since on the one hand, generators mostly use data that changes rapidly and frequently (a generator that makes use of nanoseconds could be expected to change internal values every single nanosecond) and it’s nearly impossible for anyone to look at this data fast and often enough (given they know what data to look for and which formulas to apply to that data in the first place), and on the other hand, this simply can’t be avoided. In theory, every generator made by humans is fully predictable and all we can do is to make the process of predicting the results of a generator as hard as possible

the one i used went based off the system clock, it pulled the milisecond numbers and used that. there are others that add number to that and other equations but it’s usally based around the micro seconds of the system clock.

For *random* numbers you start by getting a degree in math, then you argue with people who made the same investment about what “random” means.

For ordinary purposes (say a game) you want an *unpredictable* number. Back in the day we had computers with 8-bit processors and a 16-bit address space, so the maximum memory you could have, if you could afford it, was 64 kilobytes. Half of that was taken up by a BASIC interpreter. The processor ran at less than a megahertz. With precious little computing resources if you wanted a “random” number you displayed the message “Press Any Key” and started counting. The computer can count pretty high by the time the user reads the message, presses the key, and the electronics tell your code that the the key has been pressed. You stop counting and take the last few digits of the count – that number is as likely to be 37 and 74 and no one can predict it, thus it’s “random”.

Depends on the generator. Some do some math stuff to generate a seemingly random number, others use some kind of data from the environment to generate a number. For example, I believe random.org uses atmospheric noise to generate their numbers. It’s like spending all day looking up into the sky and counting the birds that fly by to generate random numbers.