When you click on links to various merchants on this site and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network.
Computers - Are Random # Generators really random?
Computer programs have advanced a lot in the past 12 years. I took a computer course in highschool and we 20 students all input a program for a random number generator. We all got the same pattern of "random" numbers.
I was talking to a friend who works with computers for a living and he said it is quite involved as to how random numbers are generated but that basically it is based on the year/month/time setting of the computer.
Okay, but how?
Wouldn't the "pattern" be the same on the same day next year? or when the year/month/time number was added the same?
Can anyone explain it? (I believed him when he told me but I was tired and didn't retain it)
No they are not true random numbers, they are known as pseudorandom numbers. How they work is a little beyond me, but I do remember as a professor was trying to explain it he had mentioned that somewhere (MIT maybe?) they pointed photocells or something similar at lava lamps to help a computer generate true random numbers.
I don't know how to explain how it works but in Visual Basic, you not only have to request your random number ("Rnd" command) that the program is to generate, but you have to insert the "Randomize" when the program loads.
This is what one of my books says about it: "The Randomize statment is added to the program and will be executed each time the program starts. Randomize uses the system clock to create a truly random starting point, or seed, for the Rnd statement used in the Command1_Click procedure."
Your friend probably confused 'system clock' with date, time etc.
If you double click your clock and actually watch it while your computer is busy, you'll probably notice that the seconds will lag behind once in a while, and then it will catch up, the 'system clock' is keeping track of all that.
What is the system clock?
In computers, sequence is everything. The system clock synchronizes the tasks in a computer, like loading data before manipulating it, etc. The system clock is a circuit that emits a continuous stream of precise high and low pulses that are all exactly the same length. One clock cycle is the time that passes from the start of one high pulse, until the start of the next. If several events are supposed to happen in one clock cycle, the cycle is subdivided by inserting a circuit with a known delay in it, thus providing more highs and more lows.
Last edited by furball69; Aug 23, 2006 at 09:28 PM.
There was some talk last year of using the cooling rates of resistors to generate random numbers, but I don't know what ever became of that. Generating a truly random number with a computer is pretty difficult, since everything in a computer is based off known values.
I guess one thing that could be done is using a seed like how many miliseconds the computer has been turned on at the time the random call is made, and go from there using whatever calculations need to be made to get the desired type of number.
To further add to my explanation, the fact that a computer program, that is written to pick a random number (without the 'randomize' statement), is meant to reproduce the same results by default. It is a feature of the programming language so that you can test the program while it is in development to see if your program is putting out expected results throughout the run of the program.
I think I read somewhere that some of the randomizers are based on the number of miliseconds since Ceasar's last breath - a number that will always be different.
On that note, I modified a practise program last night to generate random numbers and count how many times certain numbers came up. It's a computer slot machine that's been highly modified.
I know the program works on my computer because I have the Development environment installed on it but there's a chance that there may be some missing DLL's or OCX's on other computers. Let me know which ones, if any so I can include them in the zip.
Standard disclaimer; there's no malware of any sort on the program and using the program is at your own risk.
Last edited by furball69; Sep 29, 2006 at 11:38 AM.
The simple way to think of it is each program has a huge set list of "random" numbers. This list is huge. When asked for a random number, you need to supply the computer a "seed", which is basically a starting point in that list. It grabs the random number from that location. The next time you ask for a random number it simply grabs the next number on the list, unless you "reseed" the random number function.
So by using the date and time and any other combination of changing numbers to create the seed, you get a random, non-reproducible set of numbers.
On the other hand, if you use the same seed everytime, you'll get the same set of "random" numbers everytime.
One thing I did many years ago with a program that required random numbers was to reseed the generator with the number of milliseconds between the keystrokes of the users. Its as close to random seeding as you can get on a PC without having to reference external sources.
I thought I'd post the "meat and potatoes" of teh code that makes that program work, just for fun, so snyone interested in what a program looks like and what it does every time it cycles.
If RadioButton1.Checked = False Then GoTo custom End If PictureBox1.Visible = False Label1.Text = CStr(Int(Rnd() * 10)) Label2.Text = CStr(Int(Rnd() * 10)) Label3.Text = CStr(Int(Rnd() * 10)) If (Label1.Text = "7") Or (Label2.Text = "7") _ Or (Label3.Text = "7") Then PictureBox1.Visible = True Beep() End If GoTo done custom: Try Label5.Text = "0" Label6.Text = "0" Label7.Text = "0" Label8.Text = "0" rolls1 = TextBox1.Text If rolls1 < 0 Then rolls1 = (rolls1 - rolls1 - rolls1) End If doubles1 = 0 triples1 = 0 singles1 = 0 nosevens = 0
Do Until rolls1 = 0
L1 = CStr(Int(Rnd() * 10)) L2 = CStr(Int(Rnd() * 10)) L3 = CStr(Int(Rnd() * 10)) If L1 <> 7 And L2 <> 7 And L3 <> 7 Then nosevens = nosevens + 1 GoTo loopfin End If If L1 = 7 And L2 = 7 And L3 = 7 Then triples1 = triples1 + 1 GoTo loopfin End If If L1 = 7 And L2 = 7 Or L1 = 7 And L3 = 7 Or L2 = 7 And L3 = 7 Then doubles1 = doubles1 + 1 GoTo loopfin End If If L1 Or L2 Or L3 = 7 Then singles1 = singles1 + 1 GoTo loopfin End If loopfin: rolls1 = rolls1 - 1 Loop Label1.Text = L1 Label2.Text = L2 Label3.Text = L3 TextBox1.Text = rolls1 Label5.Text = triples1 Label6.Text = doubles1 Label7.Text = singles1 Label8.Text = nosevens Label9.Text = (triples1 + doubles1 + singles1 + nosevens) Catch MsgBo"Make sure you only enter numbers.", MsgBoxStyle.OKOnly, "Incorrect Input") End Try If (Label1.Text = "7") Or (Label2.Text = "7") Or (Label3.Text = "7") Then PictureBox1.Visible = True Beep() End If done: