How do computers randomise/shuffle?

643 views

I don’t understand how an algorithm can be truly random

In: Technology

8 Answers

Anonymous 0 Comments

Computers don’t actually use true randomness. They use an algorithm which is actually completely deterministic, but *looks* random, called a pseudorandom number generator, or PRNG. A PRNG takes an inital value, called a seed, and then derives an infinite sequence of values from that initial value.

Because PRNGs are deterministic, reusing the same seed results in the same sequence. Let’s say you have a PRNG, and use zero as your seed, then generate 5 values: 10, 0, 1, 99, 24. After that, you forget the values and start over, seeding with zero again. You’ll get 10, 0, 1, 99, and 24 again, because those 5 random looking values were deterministically derived from the seed.

In order to generate a different set of values, you have to use a different seed. One of the easiest ways to do this is just to use the current time as the seed. This is computer time, not clock time. Computers measure time by counting the number of seconds passed since Jan 1, 1970. Because this number is constantly increasing, PRNGs seeded with the current time will constantly generate new pseudorandom sets.

Anonymous 0 Comments

It can’t. At least an algorithm alone can’t.

For “mundane” purposes, you just use a “kinda random” source of data like the last digits of the current time in milliseconds, the processor clock speed or the mouse cursor position, and then run it through a chaotic function (one that maps similar inputs to completely different outputs, but is still predictable). This is called a PRNG, or pseudo random number generator.

For security critical purposes, you have to use something else but software. And people get creative here: there’s for example a literal wall of lava lamps with a webcam pointed at it, and that webcam’s video stream is the randomness source.
Obligatory Tom Scott video: https://www.youtube.com/watch?v=1cUUfMeOijg

Anonymous 0 Comments

That’s the thing – it can’t.

Computers are, by design, not random. Everything that you do on a computer is represented as data, and that data is stored physically as ones and zeroes, representing an electrical switch in either the on (1) or off (0) position. With enough of these switches, you can store information as a stack of these switches, and read it by translating the ones and zeroes into something that we can understand, like basic English. This is a painfully simplified version, but the important part here is that there is, 100%, no randomness here. If there were, the data you want to store or read would be corrupted, and therefore lost.

Computers get around this by making incredibly complicated algorithms that take a starting variable, run it through a mathematical gauntlet, and then display the new result as the “random” number. An example of this would be rolling a dice for 1-6 in a gambling video game.

Instead of running a physics simulation to determine how the dice lands, a computer will grab a random number from somewhere on the system. This is ideally a number that can change a lot over time, like the players current score added to the current time. That initial variable is then put into an algorithm, usually a long one that is likely to change the variable significantly, without reproducing the same numbers over and over. Then, this number is divided and rounded down until it’s a number between 1-6, which is then displayed to you as your result.

This is usually how RNG is programmed, but here’s the thing: if you know what the algorithm is or how it works, and you can predict or manipulate the starting variable, you *can predict every time* what the “random” number will be, with perfect accuracy.

A famous example of bad RNG would be the a Fire Emblem game for the Gameboy Advance. Instead of an algorithm or some kind of generator to create random numbers, it literally has a list of numbers that it goes down, one by one, every time something “random” needs to happen. If you know the table, you can use useless actions to burn through the low numbers or make your enemies use them, and then attack when the large numbers are next on the list.

There are quite a few videos that explain this way better than I can, go look up RNG on YouTube.

Edit: I love it when a comment spurs a bunch of good discussion. Just a reminder that my explanation is pretty quick and dirty, I skipped over A LOT in this explanation. Computerized RNG is a fascinating topic.

Anonymous 0 Comments

There’s no actual ELI5 in here, so I’m going to give it a try.

They don’t, not really, but they do pretend. They have a lot of different ways to pretend, and a lot of them pretend a little different fron each other.

One of them might pretend like you’re throwing a bunch of numbers in a hat, and picking numbers out before putting them back. Sometimes they even write the picks down in order and hide the picks from you until you need a number. Then they go down the list when you ask for a number.

Another one might be waiting for you to tell it to give you a number. It doesn’t have one, so it has to make some up. Maybe it looks at the time on the screen for a clue. Maybe it sees that there are some things on the screen, so it uses that instead. But it doesn’t want you to know it’s just looking at stuff, so maybe it adds or subtracts based on what time it is. “I have ten things on the screen, and it’s six at night, so I’ll take six out of ten and get four. But he might figure that out, so I’ll add one back and get five. I’m so smart.”

Some might have a cup of dice always shaking, or a spinning wheel always turning, and it drops them when you ask for a number. The people who make these computers want it to feel like you’re really getting numbers because of luck, so they work hard to hide that it can’t just make a number up. They’re really, really good at pretending now.

Anonymous 0 Comments

They really can’t. Computers are 100% logical, they work by changing things in a specific order they already know. If your name is Jonathan, a computer will randomize it to Hatanjoh and we could call it a day, but the computer actually thought about changing it that way.

Anonymous 0 Comments

The most common way to generate a random number is the linear congruential generator (https://en.wikipedia.org/wiki/Linear_congruential_generator). Even though it doesn’t give very good results and is obsolete a lot of people still use it because it is simple. Many programming languages (gcc, Java, Delphi, Visual Basic, Visual C++) still use/used it as their default.

First you generate a seed, which is either the current time in seconds or time since the computer booted in seconds.

Then you do the formula:

seed = (a * seed + c) % m

Seed becomes you new random number. Java for examples uses the values a = 25214903917 c = 11 m = 2^48

Anonymous 0 Comments

Most of the time algorithms are used to create “nearly” random numbers, such that no simple look will notice any pattern.

Sometimes a microphone or other input can seed the algorithm. This helps.

Anonymous 0 Comments

A very well-written article on Quanta about a question similar to this one:

[https://www.quantamagazine.org/how-and-why-computers-roll-loaded-dice-20200708/](https://www.quantamagazine.org/how-and-why-computers-roll-loaded-dice-20200708/)