How does a computer program generate random numbers? Example: when you ask Siri to give you a random number between 1 to 10, how does it come up with that number?

748 viewsMathematicsOther

How does a computer program generate random numbers? Example: when you ask Siri to give you a random number between 1 to 10, how does it come up with that number?

In: Mathematics

18 Answers

Anonymous 0 Comments

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.

You are viewing 1 out of 18 answers, click here to view all answers.