QUOTE(y0ttabyte @ 20 May, 2008 - 07:14 PM)

i have a problem, my code always generate a same number(2 var random) while in same event.
this exam my code :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim one As New Random
Dim oneResult As Integer = one.Next(1, 7)
textbox1 = satuResult
Dim two As New Random
Dim twoResult As Integer = two.Next(1, 7)
textbox2.text = twoResult
(mostly they generate a same number, why?)
y0ttabyte,
I believe you are getting the same numbers (usually) because of the seed. RANDOM works by running a value (called a seed) against its algorithm. If you do not specify what that seed is, it will be based on the system clock. Because you are creating 2 RANDOM objects so close to each other, the seed will usually be the same, which means you will often get identical results.
If there isn't a need to create two separate RANDOM objects, you could just try the following:
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim one As New Random
TextBox1.Text = one.Next(1, 7)
TextBox2.Text = one.Next(1, 7)
End Sub
Alternately, if you want two RANDOM objects, you can specify their seed values when you initialize the RANDOM object like this:
CODE
Dim one As New Random(My.Computer.Clock.TickCount)
TextBox1.Text = one.Next(1, 7)
Dim two As New Random(My.Computer.Clock.TickCount + 1)
TextBox2.Text = two.Next(1, 7)
Another "random" function you may want to consider is the "RND" function. The syntax is a bit more complicated, but it seems to work better for me. If you look up the RND function in the MSDN Help, you'll find a lot of information about how it works.
The syntax is: "value = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))". If you want, you could specify the seed value by putting it inside the parenthesis immediately after "Rnd".
For getting random values between (and including) 1 and 6 for the 6-sided dice:
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Lowerbound, Upperbound As Integer
Lowerbound = 1
Upperbound = 6
TextBox1.Text = CInt(Int((Upperbound - Lowerbound + 1) * Rnd() + Lowerbound))
TextBox2.Text = CInt(Int((Upperbound - Lowerbound + 1) * Rnd() + Lowerbound))
End Sub
Personally, I like my code to look nice and simple whereever possible so that when I come back in a year or so I can still easily figure out what was going on. I would probably create a separate function that would hold all the complicated looking code and leave the Button_Click subroutine easier to read:
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Lowerbound, Upperbound As Integer
Lowerbound = 1
Upperbound = 6
TextBox1.Text = GetRandomValue(Lowerbound, UpperBound)
TextBox2.Text = GetRandomValue(Lowerbound, UpperBound)
End Sub
Function GetRandomValue(ByVal Lowerbound As Integer, ByVal Upperbound As Integer) As Integer
Return CInt(Int((Upperbound - Lowerbound + 1) * Rnd() + Lowerbound))
End Function
I hope this helps you out!
-Rob