Ryker Wells
Member
This:
Though I've encountered Random numbers in Actionscript, with VB it seems to be throwing me for a "Loop" (haha).
What I'm doing is trying to generate a number 1 through 4 which was easy enough but next what i want to do is assign four variables with the four generated numbers which i do. My next problem is making sure each different variable gets a different number which is a nightmare for me. I tried doing a For loop to see if it would keep generating until it was a completely different number though it would be a 25% chance to get that possibility (actually less because the other three numbers can be the same at this point) i give it 1,000,000 and still 1/4 of the time the first number is the same as one of the other three.
Now I'd assume what it was doing wrong but i really just have no idea... Any help?? it will be très apprécié!
Figured it out, figured I'd share if anyone wanted to know. instead of generating a number a ton of times i generated one number and in each button assigned in a number button one is 1 button two is 2 so that way if the generated number is 1 and you click button one then you get XP and if you click two you dont. Works much better!
VB.NET:
Public Class Form1
Dim Taken As Integer
Dim Numberone As Integer
Dim Numbertwo As Integer
Dim Numberthree As Integer
Dim Numberfour As Integer
Dim Takenone, Takentwo, Takenthree, Takenfour As Integer
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Private Sub Generate_btn_Click(sender As System.Object, e As System.EventArgs) Handles Generate_btn.Click
Numberone = GetRandom(1, 4)
Takenone = Numberone
Numbertwo = GetRandom(1, 4)
Takentwo = Numbertwo
Numberthree = GetRandom(1, 4)
Takenthree = Numberthree
Numberfour = GetRandom(1, 4)
Takenfour = Numberfour
For i As Integer = 0 To 1000000
If (Takenone = Takentwo Or Takenone = Takenthree Or Takenone = Takenfour) Then
Numberone = GetRandom(1, 4)
Takenone = Numberone
ElseIf (Takenone <> Takentwo Or Takenone <> Takenthree Or Takenone <> Takenfour) Then
Exit For
End If
Next
Numberone_txt.Text = Numberone
Numbertwo_txt.Text = Numbertwo
Numberthree_txt.Text = Numberthree
Numberfour_txt.Text = Numberfour
End Sub
End Class
Though I've encountered Random numbers in Actionscript, with VB it seems to be throwing me for a "Loop" (haha).
What I'm doing is trying to generate a number 1 through 4 which was easy enough but next what i want to do is assign four variables with the four generated numbers which i do. My next problem is making sure each different variable gets a different number which is a nightmare for me. I tried doing a For loop to see if it would keep generating until it was a completely different number though it would be a 25% chance to get that possibility (actually less because the other three numbers can be the same at this point) i give it 1,000,000 and still 1/4 of the time the first number is the same as one of the other three.
Now I'd assume what it was doing wrong but i really just have no idea... Any help?? it will be très apprécié!
Figured it out, figured I'd share if anyone wanted to know. instead of generating a number a ton of times i generated one number and in each button assigned in a number button one is 1 button two is 2 so that way if the generated number is 1 and you click button one then you get XP and if you click two you dont. Works much better!
Last edited: