how do random numbers on computers work?

1.40K views

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

In: 47

75 Answers

1 2 6 7 8
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.

Anonymous 0 Comments

Here’s a “formula for a random number” from 0 to 9: Multiply by 3, add 7, divide by 10 and keep remainder, repeat. So starting from 0, you get **0** -> 0 -> 7 -> **7** -> 21 -> 28 -> **8** -> 24 -> 31 -> **1** -> 3 -> 10 -> **0** -> …

Most people didn’t start with a simple example, making things confusing. You’ll notice that this PRNG (pseudo-random number generator) has some traits:

-It repeats itself after some time. That’s the “period” – here it’s 4.

-It has a consistent sequence – if you started at 7, the next number would always be 8. You can call the starting number the “seed”.

-With some cleverness, you can use it to derive numbers in a range smaller or larger than 0 to 9 (e.g. by checking if it’s odd/even, or by using it twice in a row for two digits for 00-99).

-It’s kind of very bad.

Actual PRNGs work like this, but better. You’ll notice that I picked the numbers 3 and 7 (on a whim), but some numbers are definitely worse (like multiplying by 1 and always adding 1), and some are better. Using math, you can investigate the how or why, and make better and/or faster generators.

Anonymous 0 Comments

Here’s a “formula for a random number” from 0 to 9: Multiply by 3, add 7, divide by 10 and keep remainder, repeat. So starting from 0, you get **0** -> 0 -> 7 -> **7** -> 21 -> 28 -> **8** -> 24 -> 31 -> **1** -> 3 -> 10 -> **0** -> …

Most people didn’t start with a simple example, making things confusing. You’ll notice that this PRNG (pseudo-random number generator) has some traits:

-It repeats itself after some time. That’s the “period” – here it’s 4.

-It has a consistent sequence – if you started at 7, the next number would always be 8. You can call the starting number the “seed”.

-With some cleverness, you can use it to derive numbers in a range smaller or larger than 0 to 9 (e.g. by checking if it’s odd/even, or by using it twice in a row for two digits for 00-99).

-It’s kind of very bad.

Actual PRNGs work like this, but better. You’ll notice that I picked the numbers 3 and 7 (on a whim), but some numbers are definitely worse (like multiplying by 1 and always adding 1), and some are better. Using math, you can investigate the how or why, and make better and/or faster generators.

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.

Anonymous 0 Comments

Here’s a “formula for a random number” from 0 to 9: Multiply by 3, add 7, divide by 10 and keep remainder, repeat. So starting from 0, you get **0** -> 0 -> 7 -> **7** -> 21 -> 28 -> **8** -> 24 -> 31 -> **1** -> 3 -> 10 -> **0** -> …

Most people didn’t start with a simple example, making things confusing. You’ll notice that this PRNG (pseudo-random number generator) has some traits:

-It repeats itself after some time. That’s the “period” – here it’s 4.

-It has a consistent sequence – if you started at 7, the next number would always be 8. You can call the starting number the “seed”.

-With some cleverness, you can use it to derive numbers in a range smaller or larger than 0 to 9 (e.g. by checking if it’s odd/even, or by using it twice in a row for two digits for 00-99).

-It’s kind of very bad.

Actual PRNGs work like this, but better. You’ll notice that I picked the numbers 3 and 7 (on a whim), but some numbers are definitely worse (like multiplying by 1 and always adding 1), and some are better. Using math, you can investigate the how or why, and make better and/or faster generators.

1 2 6 7 8