four different generated numbers?

Ryker Wells

Member
Joined
Feb 9, 2012
Messages
16
Location
Fort Kent, ME
Programming Experience
1-3
This:
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:
here number range 1-4 is returned randomly:
Dim rnd As New Random
Dim numbers = Enumerable.Range(1, 4).OrderBy(Function() rnd.Next)

Then you can iterate that using a For Each loop.
If you need indexed lookup of each value you must do a ToArray call, if not the Linq expression will re-evaluate each time you access an element.
Also a note about the Random class, in example a local variable was used, it you intend to call it repeatedly use a single instance of the Random class by making it a private class field.
 
Doesn't the system.random class have to be inhereted? Anyway JohnH is right I would rescript a working module for ya however I am away from my Dec station and on a tablet until 7Pm Arizona time (No daylight savings time here) if you don't get it running by the time I get home I will gladly post a working module for you.
 
10-4 I was just curious because the first random number function I wrote had it inhereted I'm sure of that, guess it was unneccesary I'm sorry if my previous reply came off condescending surely that was not my intention I was just asking a question and offering to help as I remember reading one of rykers other threads in which he specified he was new to VB.net
 
Back
Top