felipet

I am trying to make digital dice.

Does anyone have any clue as to how to make my two random numbers different.

I know how to make the random numbers, but they are always the same.

Thanks



Re: Visual Basic Express Edition How do you get two different random numbers?

Solitaire

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dieone, dietwo As Integer

Dim randnum As Random = New Random()

dieone = randnum.Next(1, 7)

dietwo = randnum.Next(1, 7)

MessageBox.Show("Die One = " & dieone & " Die Two = " & dietwo)

End Sub






Re: Visual Basic Express Edition How do you get two different random numbers?

js06

There must be something with your code

The idea that you are making 2 random numbers and they always come up the same goes against the odds

This will give you 2 random numbers that will never be the same

i thought in a dice game, they would all be 1 to 6 and it would be possible to roll the same number with several dice

I guess not all have to be this way.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim randomnum As Integer = New Random().Next(1, 7)

Dim randomnum2 As Integer = New Random().Next(1, 7)

Do Until randomnum2 <> randomnum

randomnum2 = New Random().Next(1, 7)

Loop

MsgBox(randomnum.ToString & " " & randomnum2.ToString)

End Sub

Edit

i see that solitaire has some cleaner code, she always does.

Solitaire, thanks for the help one the top ten array thing. I got it working perfect with some modifications to your code.

The only thing i noticed is that solitaire's code will still give you the same numbers on both dice if that's what you are trying to avoid then you can use her clean code and add the do until.






Re: Visual Basic Express Edition How do you get two different random numbers?

felipet

Thank you very much.

It worked perfectly.





Re: Visual Basic Express Edition How do you get two different random numbers?

nobugz

The fundamental problem in the code shown here is that the random number generator gets initialized over and over again with the New statement. That's kinda okay in a button click event unless you could click the button twice within 15 milliseconds. I guess that's possible. Put this code at the class level, outside of a class method:

Dim mGenerator As New Random

The random generator now gets initialized only once, producing truly random numbers each time you call mGenerator.Next() in your code.





Re: Visual Basic Express Edition How do you get two different random numbers?

JohnWein

New Random() without a seed is randomized with a high resolution time variable. Each sequence is random.



Re: Visual Basic Express Edition How do you get two different random numbers?

JohnWein

nobugz:

I misread your post. I thought you said It wasn't OK in a button click event. You set me straight in this thread: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1802594&SiteID=1