how do random numbers on computers work?

1.41K views

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

In: 47

75 Answers

Anonymous 0 Comments

There are two types of random numbers in computers.

The first, is “pseudorandom” (meaning “sort of random”) numbers which, like you suggest is done using an algorithm. They start with a number (maybe they start with the date and time the program started), and then they do a calculation to generate a number. The next time you ask for a number, they perform the same calculation to get a new number, and so on… You start with some number and from there you get a series of numbers that look random. Importantly, whenever you start with the same number, you get the same series of random-looking numbers.

The other way to do it is a bit slower, but it involves grabbing bits of data from different parts of the computer, like variations in the CPU temperature, time intervals between network packets, jitter in mouse movements, etc. things happening in the computer that are themselves kind of random, but combined can be used to generate a random number. This is slower because it depends on monitoring several bits of computer activity instead of just a quick bit of arithmetic.

Anonymous 0 Comments

There’s no formula for random numbers, as formulas will output the same result for the same input. Computers are deterministic, but random numbers require true randomness, which a function will never provide you with.

So you either use real random number generators that for example listen to cosmic background radiation, but they are slow and more expensive. If security is of high importance something like with will get used.

Or you can use pseudo random number generators. A simple one is a function like Y = (a * X + b) mod c where X is either the seed (for example the current time in milliseconds) or the last result. They will get used in applications where security isn’t as important but speed is, for in video games.

Let’s use an easy example (in the real world we would use larger and better numbers): a = 7, b = 3, c = 5

X = 0 -> 3 mod 5 = 3

X = 3 -> (7*3 + 3) mod 5 = 4

X = 4 -> (7*4 + 3) mod 5 = 1

X = 1 -> (7 + 3) mod 5 = 0

In this example we picked bad numbers as it would just cycle between 3, 4, 1 and 0 without ever producing a 2. You want to set a, b and c to values that result in every possible result coming out with the same frequency and in an order that appears to be random.

As you see it would always produce the same cycle of numbers so a pseudo random number generator will never be completely secure. If you initialize it with for example the current time an attacker could just try to pinpoint that time and end up with the same set of results as your application.

Anonymous 0 Comments

There are two types of random numbers in computers.

The first, is “pseudorandom” (meaning “sort of random”) numbers which, like you suggest is done using an algorithm. They start with a number (maybe they start with the date and time the program started), and then they do a calculation to generate a number. The next time you ask for a number, they perform the same calculation to get a new number, and so on… You start with some number and from there you get a series of numbers that look random. Importantly, whenever you start with the same number, you get the same series of random-looking numbers.

The other way to do it is a bit slower, but it involves grabbing bits of data from different parts of the computer, like variations in the CPU temperature, time intervals between network packets, jitter in mouse movements, etc. things happening in the computer that are themselves kind of random, but combined can be used to generate a random number. This is slower because it depends on monitoring several bits of computer activity instead of just a quick bit of arithmetic.

Anonymous 0 Comments

There are two types of random numbers in computers.

The first, is “pseudorandom” (meaning “sort of random”) numbers which, like you suggest is done using an algorithm. They start with a number (maybe they start with the date and time the program started), and then they do a calculation to generate a number. The next time you ask for a number, they perform the same calculation to get a new number, and so on… You start with some number and from there you get a series of numbers that look random. Importantly, whenever you start with the same number, you get the same series of random-looking numbers.

The other way to do it is a bit slower, but it involves grabbing bits of data from different parts of the computer, like variations in the CPU temperature, time intervals between network packets, jitter in mouse movements, etc. things happening in the computer that are themselves kind of random, but combined can be used to generate a random number. This is slower because it depends on monitoring several bits of computer activity instead of just a quick bit of arithmetic.

Anonymous 0 Comments

True randomness doesn’t exist.

If you knew all the forces acting on a coin flip (thumb flipping power, air density, wind pressure, mass of the coin, gravitational pull of earth, moon and sun, even the pressure of photons shining on the coin and all of the othera we aren’t even aware of) you’d be able to predict the outcome with a 100% accuracy every time.

Not just a coin flip, if you knew the positions and forces acting on all of the atoms everywhere, and you could calculate how they could react from said forces in real time, you could accurately predict the future of anything really.

So in order to simulate randomness people use 2 different ways to generate random numbers on a computer.

The lazy way: They have a predetermined list of numbers that they just keep giving you the next number in line every time you ask for a random number. Sometimes they add an equation on top of the predetermined list to spice things up.

The random but not really way: Instead of using a predetermined list, they just borrow a number from a source that keeps generating different numbers. Sometimes it’s the scrambled digits of the millisecond of the moment you requested the random number, sometimes it’s the scrambled numbers of a random sensor in your pc, depends on the person who made the number generator really.

Anonymous 0 Comments

The first (and easiest to understand) method of generating random numbers did indeed use a formula. Given the previously generated number `x`, a random seed `c`, a multiplier `a`, and a modulus m, you can compute a pseudorandom number as `(a * x + c) mod m`, where mod m means “divide by m and take the remainder”. See [https://en.wikipedia.org/wiki/Linear_congruential_generator](https://en.wikipedia.org/wiki/Linear_congruential_generator) for more details

Nowadays, we have more complex algorithms, but they’re a lot harder to understand without going really in depth. If you’re curious, wikipedia also has good explanations of them: [https://en.wikipedia.org/wiki/Mersenne_Twister](https://en.wikipedia.org/wiki/Mersenne_Twister), [https://en.wikipedia.org/wiki/Xorshift](https://en.wikipedia.org/wiki/Xorshift), [https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear](https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear)

Anonymous 0 Comments

aside from many of the answers for modern day methods, there was a retro game development video I watched which went over how they used to get random numbers.

first was obviously, use maths or statistics but this was expensive. second was to use the current frame which in those days was not guaranteed as frames were computed last. the last one, most interestingly, used in I believe ff4 (made by one guy) literally sequentially went through a static set of results.

Anonymous 0 Comments

The first (and easiest to understand) method of generating random numbers did indeed use a formula. Given the previously generated number `x`, a random seed `c`, a multiplier `a`, and a modulus m, you can compute a pseudorandom number as `(a * x + c) mod m`, where mod m means “divide by m and take the remainder”. See [https://en.wikipedia.org/wiki/Linear_congruential_generator](https://en.wikipedia.org/wiki/Linear_congruential_generator) for more details

Nowadays, we have more complex algorithms, but they’re a lot harder to understand without going really in depth. If you’re curious, wikipedia also has good explanations of them: [https://en.wikipedia.org/wiki/Mersenne_Twister](https://en.wikipedia.org/wiki/Mersenne_Twister), [https://en.wikipedia.org/wiki/Xorshift](https://en.wikipedia.org/wiki/Xorshift), [https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear](https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear)

Anonymous 0 Comments

True randomness doesn’t exist.

If you knew all the forces acting on a coin flip (thumb flipping power, air density, wind pressure, mass of the coin, gravitational pull of earth, moon and sun, even the pressure of photons shining on the coin and all of the othera we aren’t even aware of) you’d be able to predict the outcome with a 100% accuracy every time.

Not just a coin flip, if you knew the positions and forces acting on all of the atoms everywhere, and you could calculate how they could react from said forces in real time, you could accurately predict the future of anything really.

So in order to simulate randomness people use 2 different ways to generate random numbers on a computer.

The lazy way: They have a predetermined list of numbers that they just keep giving you the next number in line every time you ask for a random number. Sometimes they add an equation on top of the predetermined list to spice things up.

The random but not really way: Instead of using a predetermined list, they just borrow a number from a source that keeps generating different numbers. Sometimes it’s the scrambled digits of the millisecond of the moment you requested the random number, sometimes it’s the scrambled numbers of a random sensor in your pc, depends on the person who made the number generator really.

Anonymous 0 Comments

True randomness doesn’t exist.

If you knew all the forces acting on a coin flip (thumb flipping power, air density, wind pressure, mass of the coin, gravitational pull of earth, moon and sun, even the pressure of photons shining on the coin and all of the othera we aren’t even aware of) you’d be able to predict the outcome with a 100% accuracy every time.

Not just a coin flip, if you knew the positions and forces acting on all of the atoms everywhere, and you could calculate how they could react from said forces in real time, you could accurately predict the future of anything really.

So in order to simulate randomness people use 2 different ways to generate random numbers on a computer.

The lazy way: They have a predetermined list of numbers that they just keep giving you the next number in line every time you ask for a random number. Sometimes they add an equation on top of the predetermined list to spice things up.

The random but not really way: Instead of using a predetermined list, they just borrow a number from a source that keeps generating different numbers. Sometimes it’s the scrambled digits of the millisecond of the moment you requested the random number, sometimes it’s the scrambled numbers of a random sensor in your pc, depends on the person who made the number generator really.