how do random numbers on computers work?

1.46K views

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

In: 47

75 Answers

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.

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