It kind of doesn’t, actually.
These numbers are generated by algorithm, so they’re not truly random. To get around this, these algorithms usually take in “seed” data, which is some arbitrary piece of data to base their randomness around. This is some kind of data that’s not likely to ever be repeated–like the precise current time, or some noisy data, and CloudFlare even uses a [wall of lava lamps](https://www.cloudflare.com/learning/ssl/lava-lamp-encryption/).
Computers are generally pretty terrible at coming up with truly random data; this is a consequence of the fact that they operate on known inputs in known programmatic ways.
When you talk about randomness in the context of computers, then, what you’re mostly always talking about is something called “pseudo-randomness” — output that appears “close enough” to truly random for most purposes.
Pseudo-random number generators (PRNGs) perform one or more mathematical operations on a number, then return that result, as well as *using* that result as the starting value for the next “random” number. Depending on the complexity of the operations and how they’re chained together, you can get a pretty random-seeming sequence, but make no mistake; they *aren’t* random.
Most computers use pseudo-random number generation. They start with a number, called the “seed” that they get from somewhere (often the exact time when the request is made, represented as the number of milliseconds from some fixed reference time). Then they run that number through a bunch of mathematical functions. It’s not really important what these functions are so long as, to a human, it’s very hard to predict what number will come out.
So, without knowing what code Siri actually uses, a rough approximation of the process is:
1. Siri notes that the time is 2:10 and sets the seed, S, to 210
2. Siri calculates X = floor(sqrt(S^3/150 + 10709))
3. Turns out X is 269 (could you have guessed?)
4. Siri returns the last digit, 9
To get another number, Siri can use the one it just generated as the new seed. In principle, if you know the code in the random number generator and you know the seed, you can predict not just the next number but the whole series of numbers it will generate. This is fine for casual purposes like games and simple decisions, but it’s not great for high-security things like cryptography. In those cases, they use “truly random” sources, like the weather or (no joke) a lava lamp.
It doesn’t. Computers use what’s called psuedorandom numbers. Basically, it is a very complex algorithm that takes in a big number as what we call a seed, and it uses that generate the random number.
However, if you call the random number function with the same seed every single time, it will generate the same sequence of random numbers every single time.
Sometimes, this is done with the system time as the seed, but again, you can call the function with the exact same system time and get the same number. If that random number needs to be unpredictable, this is unacceptable (like encryption).
For truly random numbers, we have a facility in California run by a company called Cloudflare. In this facility there is a wall of lava lamps with a camera pointed at them, and when someone needs a truly random number, they send a request to Cloudflare. Their camera takes a picture of the lava lamps, and uses the position of the lava to generate a random number.
No one as a computer powerful enough, nor knows the initial conditions of these lava lamps well enough simulate the lava lamp wall accurately enough for anyone to predict the random numbers generated, so these random number are secure.
Some software generates very poor random numbers. They have a number called a seed… every time you ask for a new random number, the seed is multiplied by a huge number, then a big number is added, and the result is your next “random” number.
And that’s just a sign of the problem: historically, CPUs don’t generate random numbers. Given the same information as before, and the same sequence of instructions being followed, you will always get the same result. The same seed always produces the same sequence of “random” numbers. It’s common to start the seed at some unique value, like the current date+time, but if you know what those are and the math involved, random numbers can still be predicted.
For actual randomness, some external source of “entropy” is required. Various sources exist. CPUs can measure time to nanoseconds since they run so fast and have a counter of how many instructions they’ve executed… reading that number to the full nanosecond has a degree of uncertainty when you check the exact number. If you measure the time between keyboard key presses from the user, but also down to the nanosecond, that’s difficult to predict. Some people who need random numbers for very specific workloads have been known to do weird things, like point a camera at a wall of lava lamps, or attach a Geiger counter (radiation detector) to a microphone connector, or just tune a radio to an unused frequency, crank the volume and attach that to the microphone.
Modern CPUs have a random number generator based on some kind of hardware measurement not directly available to the user and whatever algorithm the CPU manufacturer wants to use.
These are more concerning for things like encryption which depends on unpredictable data being used and typically never re-used. But it’s just as available for something like Siri to answer your question.
In very basic terms, it takes the exact date&time down to the millisecond and calculates that down to the interval that is requested. However, if you know the exact math and time input used, you can recreate the result, making the system imperfect.
Nature is much better at randomness. Lavarand for instance is a system that uses live camera footage from lava lamps to create a random pattern. It’s still being used today.
Usually it’s done using a Pseudo Random Number Generator (PRNG). This algorithm takes a number as input and outputs another seemingly unrelated and usually irreversible number.
However, this algorithm isn’t inherently random, as the same input will always produce the same output, so you need to start the algorithm with a number you trust to be random enough (called a seed) such as the current time with millisecond precision, the current temperature of the CPU, camera noise, the position of the mouse cursor or a combination of all of those.
Then, the previous result of the PRNG is fed back into it, generating a sequence of numbers that looks completely random.
There are a lot of different ways!
Sometimes they do math on a changing number, like what time you asked for a random number.
Sometimes they look one up on a list of random numbers
Sometimes they use something else.
For the math one you could do something like take the time in milliseconds, and take the last 10 digits. Put a decimal place in front so it’s in a zero to one range. Now multiply it by the range you want a random number in. Done!
If you want you can do more math to the numbers to change them.
Others have described the different way “random” numbers are generated.
I’ll add two things:
1) from a programmer’s perspective, most computer languages have a built-in function and/or set of libraries for generating random numbers. You may not know what words like “function” or “library” mean in this context, and you don’t have to. The important part is that, as a programmer, I don’t have to know all the mathematical details. The tools I use to create programs make that easy for me: within my code I can basically say “give me a number between 1 and 10” and the system does it. I realize this is kind of just pushing your question to a different level, the point is just that we have ways to make all this easy for the people who write computer programs. That comes with a cost, though. If there’s something wrong with this easy way of generating random numbers in a particular programming language, it can affect lots and lots of programs all at once. As a programmer, I’m depending on whoever created that random number system. If I’m writing a super critical system that handles lots of money or the nuclear launch codes or something (disclaimer: I have never worked on nuclear launch codes), I may need to be a lot more careful than just using the easy system. On the other hand, if I’m writing a solitaire game, the easy system is probably just fine.
2) it may seem at first glance like the “pseudorandom” systems that others have described, where you get “random but not really” numbers, would not be as good. And sure, there are cases where it might be really important for a piece of software to need “as random as you can get” numbers. But there are other cases where the “pseudorandom” behavior is actually more desirable.
As an example of why pseudorandom numbers might be useful, I once needed to implement a feature that presented a series of images to the user, who would then make certain determinations about the images. (This was an opt-in feature allowing users of open source data to voluntarily help improve that data in a a gamified fashion, not the annoying captcha things that ask you which pictures contain fire hydrants or anything like that.)
It wasn’t important for the feature that the images any particular person saw be truly random. It was just important that they didn’t all see the same set of images (because that helped us get through all the images faster, among other reasons).
So we intentionally used a pseudorandom number generator using a seed based on current time. This had a couple of benefits. First, it was easy to code and it was “random enough” for our use case. But more importantly, if a user reported an issue, they could send us the url they were using, which contained the seed value. Then by using that seed, we (the programmers) could easily see exactly the same set of images the user had seen, in exactly the same order. This was helpful in fixing problems.
There would have been other ways to handle that, of course. But in our case, “pseudorandom” was a great fit for our needs.
Latest Answers