Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator.
The key point is that you need to start the random number generator with a seed. Doing it in the form load with the number of milliseconds since midnight is an easy way to start.
The VB.Net version
This demo project will be as simple as I can make it. It consists of a form with one button. Every time you push the button, a MsgBox will pop-up with a random number between 1 and 10. This code is for VB.NET and was created with VB.NET 2005 Express Edition.
Start a new project and double click the button in the toolbox to add a button to the form. Double-click the button to see the code outline:
Expand|Select|Wrap|Line Numbers
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- End Sub
- End Class
Expand|Select|Wrap|Line Numbers
- Public Class Form1
- Private oRand As Random
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- oRand = New Random(DateTime.Now.Millisecond)
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim iRand As Integer
- iRand = oRand.Next(1, 10)
- MsgBox(iRand)
- End Sub
- End Class
- oRand is the Random number Generator and is defined at the module level
- oRand is initialized once in the Form Load event using the millisecong component of the current time.
- The Next method of the Random object generates a new number.