A computer is not capable of coming up with a number out of the blue. Computers are not creative — they only do what they are told.
Therefore, there is no computer which generates a truly random number. The only true random numbers come from nature — such as frequencies put out by radioactive decay.
A computer RNG works as follows:
- It starts with a number called a seed. YOU supply this number to the RNG, normally by passing it a "tick count" of your computer's internal clock. (Specifically, the tick count is the number of milleseconds that have elapsed since the computer was turned on.)
- It keeps track of the number of random numbers that it has generated since the last time you assigned a new seed value. We'll call this the sequence number.
- The RNG performs a series of calculations involving the seed number and the sequence number, such that the end result is a number that falls between 0 and 1. (For example, 0.7236576453287423) In laymen's terms, this calculation might be something like the seed number times the sequence number, divided by 762345, plus 65, times -.04723654, divided by.............. and so on. Whatever the calculation, the result is always between 0 and 1.
- The next time you generate a random number, it performs the same calculation, except the sequence number is now 2, so the end result will be a different number between 0 and 1.
The series of calculations is more complex than I indicated in my sample above. It most likely includes constantly-changing information like the tick count in the calculation. The idea is to make it as unpredictable as possible. However, if you know the exact calculation, you can feed it exactly the same inputs as the first time it created a random number, including the seed number, the sequence, and anything else used in its calculation, then you will get exactly the same result as the "random number".
In most programming languages, when you use an RNG, the first thing you do is to "randomize" the RNG by assigning a seed value equal to the computer's tick count. Then you use the RNG however many times is necessary.
For example, using Visual Basic, if I'm generating six random numbers (for a 6/49 game), I would do it as follows:
Dim numbers(6) As Integer
Randomize() 'In VB, specifying no number will automatically use the system timer (tick count).
For x As Integer = 0 To 5
numbers(x) = CInt(Rnd() * 49) + 1
Next
Explanation:
Dim numbers(6) As Integer
→ This creates storage space to hold the siix random numbers (we have to put them some place after we create them)
Randomize()
→ This sets a new seed value equal to the computer's tick count, so these six numbers will be "unpredictable"
For x As Integer = 0 To 5
→ Creates a value called "x", initially assigned a value of zero (0). The next statement is executed 6 times, and x will be incremented each time, so the second time x will be one (1), then two (2), and so on, up to five (5).
numbers(x) = CInt(Rnd() * 49) + 1
→ Takes a random number between 0 and 1, multiplies it by 49, then removes the decimal portion (leaving just a whole number), and adds 1 to it. The result is a random number between 1 and 49. For example, if Rnd() produced 0.7236576453287423, it would be 0.7236576453287423 x 49 = 35.4592246211083727, then removing the decimal portion would give 35, and finally adding 1 would give a random number of 36.
Next
→ Marks the end of the code that is executed 6 times.
That's my primer in RNGs, I hope it was illuminating!