how do random numbers on computers work?

1.38K views

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

In: 47

75 Answers

Anonymous 0 Comments

Some microcontrollers use “shot noise” from a spare transistor to feed their Random() statement. You could feed that into a gaming system or anything really.

Lots of formulas: [Link](https://www.physics.wisc.edu/courses/home/spring2020/623/miscellaneous%20electronics%20tips/More%20noise%20references/shot_noise_chp6_txt.pdf)

Anonymous 0 Comments

Complete (theoretical) randomness is hard to achieve.

Computers often generate pseudo random numbers by taking a measurable event and calculating a new value from that seed value in an attempt to give a result that behaves like a random number.

The most common approach is to use a value like the current time to calculate a number between 0 and 1. If you have special requirements for this number you need to look after them yourself.

As a simplified example, lets say I want a random number to approximate a coin flip (two options, heads or tails)

In this case you might choose heads for any value 0.5000 and above, and tails for any value below or equal to 0.4999.

If you want to simulate dice rolls (1,2,3,4,5, or 6) you might take the “random value (0 to 1), multiply by 6 and add 0.5 and then drop the part to the right of the decimal point.

If you are programming in C++ this give a [good intro](https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/)

Anonymous 0 Comments

Computers don’t do random. They usually run a formula with the starting point based on time. It just appears random.

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.

Anonymous 0 Comments

Complete (theoretical) randomness is hard to achieve.

Computers often generate pseudo random numbers by taking a measurable event and calculating a new value from that seed value in an attempt to give a result that behaves like a random number.

The most common approach is to use a value like the current time to calculate a number between 0 and 1. If you have special requirements for this number you need to look after them yourself.

As a simplified example, lets say I want a random number to approximate a coin flip (two options, heads or tails)

In this case you might choose heads for any value 0.5000 and above, and tails for any value below or equal to 0.4999.

If you want to simulate dice rolls (1,2,3,4,5, or 6) you might take the “random value (0 to 1), multiply by 6 and add 0.5 and then drop the part to the right of the decimal point.

If you are programming in C++ this give a [good intro](https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/)

Anonymous 0 Comments

Computers don’t do random. They usually run a formula with the starting point based on time. It just appears random.

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.

Anonymous 0 Comments

Computers don’t do random. They usually run a formula with the starting point based on time. It just appears random.

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.

Anonymous 0 Comments

True randomness is not possible. In short, yes there’s a function that if you know all the inputs, you can predict the outcome.

But it can get ridiculous. It might be take the date time down to the millisecond. Convert it all to numbers. Multiply it by 12345678987654321 and then take every other digit as your output. So if you ask for a random number at a known time then you can do the math and figure out the output. But otherwise it might as well be random.

It doesn’t have to be time. It could be whatever your current IP is or the file path of the last opened file. Or literally anything could be used to insert “randomness” into the formula.