How does a computer pick a random number?
Observing members:
0
Composing members:
0
8 Answers
Different computers do it different ways, so there isn’t one answer which is universally true.
As a rule, they take some event – like the time to some large number of decimal places, and apply some algorithmic formulas that are themselves derive randomly, then do some multiplication.
As the article below describes, sometimes computers get their ‘seeds’ from thing like unpredictable mouse movements or sounds.
read this for more detail
Many random number routines are algorithmic and will always produce the same stream of pseudo-random numbers for any given seed number. To get “really” random numbers, you then put some number that’ll be different each time you run the program, into the seed value. A common way to do that is using some part of the computer’s current clock data at the exact moment the program reaches the part in the program where it does that.
Computers are incapable of producing truly random numbers on their own without being connected to some external device. They produce pseudorandom numbers that are deterministic (given the same inputs, it will always yield the same output).
@gorillapaws Eh, sort of. In some contexts, one can do pretty well at generating some randomness. Certainly enough for most general computing purposes. A game seeding a pseudo-random number generator with a number based on the current clock at launch is plenty good for most gaming purposes, for example. And more randomness can be mined from things like human input devices.
@Zaku The op explicitly mentioned the lottery. I know they use lava lamps to generate cryptographically secure random numbers for things like lotteries (maybe they use other things now). Of course you’re right about being good enough for a game. But when things are based on timing from the launch of the computer, a hacker can exploit this since the loop of pseudorandom numbers is ultimately known and they can use timing attacks to predict outcome with way more certainty than chance.
We had an algorithm that used some digits from the clock: e.g. the 3rd, 6th, and 7th digits in the seconds value and used that as a seed for the RAND algo. That result was used as a seed for the RAND algo result we actually used.
We were intentionally adding random noise to a reading so we could get higher resolution than the base device could deliver. We would then take thousands, millions of results and average to give us a high confidence reading orders of magnitude finer than our base equipment.
You can do this experiment yourself. Imagine you have a tile floor with 10 cm (4 inch) tiles and very thin grout lines. Here’s the problem: Using only the floor tell me the diameter of a penny down to 1 mm or 0.040 inches. I want you to be 95% certain the error is no more than 0.5mm or 0.020 inch
The answer is to randomly drop pennies on the tiles by flinging them all over the place and counting the number of pennies that touch the thin grout line. Multiply the percentage that touch the line by the tile spacing and you have your answer. If you want to be very sure of your answer you will need to drop 10,000 pennies.
Let’s do an easy example so you get an intuitive feel for it. Let’s say the coin is 5 cm in diameter, 2 inches. If you drop one coin the odds of it landing on the grout are 50%. Drop 10 coins randomly and you will get 5 touching the line. 5 out of 10 is 50% then multiply that by the size of the tile 10 cm to get 5 cm. But your confidence limit is large. You might also get 4 or 6 coins touching with only 10 drops. Occasionally you might get 3 or 7. So drop 100 coins and count. Drop 1000 coins and count. Drop a million . Your answer is more and more confident with every drop. This only works if your random noise is truly random and larger than your signal. You need to toss the coins over dozens of tiles.
Math works!
Of course, calipers work too.
There are various algorithms and what you do is give the program a ‘seed’ and it will use math to produce a number sequence off of that seed that SEEM random to a human but aren’t really truly random. Typically, we use a seed that varies from number to number. For example, I like to use the last N digits of the system time in milliseconds. This value will be completely unpredictable by someone issuing a request to the server as the amount of time it takes to REACH the randomizer will be unknowable based on load and timing of click and such.
If I’m writing something not UI based where I need rather alot of random numbers, I’ll often add or multiple several factors together that will be different from record to record AS WELL AS the milliseconds. As long as you’re not re-using the same seed over and over, the appearance of random numbers produced by most libraries is pretty dang good.
Response moderated (Spam)
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.