how do random numbers on computers work?

1.48K views

For example, is there a formula for a random number?

In: 47

75 Answers

Anonymous 0 Comments

Computers don’t know how to be spontaneous, so they can’t come up with TRULY random numbers by themselves. Instead, they use one of two strategies:

Sampling random noise: take something from the real world that actually is random, like the tiny fluctuations of particle densities in the air, measure that, and boom: you have random numbers. This requires special sensors though and if you want uniformly distributed randomness, where every number is equally likely, you also need a heavily controlled environment, so those who opt for this method often request their random numbers over the internet from companies which specialize in measuring randomness.

Pseudorandom numbers: this is the much more widely used strategy. We come up with a special algorithm, which takes in a number and spits out a different one. Every time we want a random number, we feed in the last random number we generated and it gives us a new one. If there is no previous random number, we just feed it the current time on the system, which if measured down to the millisecond, is unpredictable enough.

The algorithm can be anything, but it should have some specific properties:

1: we want our random numbers to be unpredictable, so there should be no way to guess what the next number will be. In other words, there should be no recognizable patterns in the sequence of numbers that it generates. (Except of course, once we’ve cycled through every possible number that a computer can represent, at which point we have to loop back to the beginning).

2: we want every number to have an equally likely chance of generating (AKA a uniform probability distribution) so for every number that the computer can represent, there should be exactly one other number that gives that number as output when we put it through the algorithm.

In practice, it takes a lot of thinking and math skills to come up with a good random number generator, so most programming languages come with one built in to a standard library that programmers can just use without having to make it themselves.

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