Results 1 to 5 of 5

Thread: [RESOLVED] Randomize from dynamic array

  1. #1

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Resolved [RESOLVED] Randomize from dynamic array

    I have a dynamic array that will be populated by the user which are strings. I also have a textbox for the user to enter a number of random items to be pulled from the array and listed in a text box on another form when a button is clicked on main form. I'm not sure how to go about getting random strings from a user generated array. Here is the code for the user input. Any help for the btnrandom_click event would be appreciated.

    Public Class frmMain
    Dim bunkname() As String
    Dim cells As Integer

    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
    Dim prompt, title As String
    Dim i As Short
    prompt = "Enter each Bunk number."
    cells = InputBox("How many total bunks do you have?", "Create array")
    If cells > 0 Then ReDim bunkname(cells - 1)
    For i = 0 To UBound(bunkname)
    title = "Cells " & (i + 1)
    bunkname(i) = InputBox(prompt, title)
    Next
    End Sub

    Private Sub btnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click
    frmList.Show()
    Randomize()

    End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Randomize from dynamic array

    You get a random element from an array the same way regardless of how it was created:
    vb Code:
    1. Dim index As Integer = myRandom.Next(0, myArray.Length)
    2. Dim element As Object = myArray(index)
    Obviously the array and the Random object must already exist. Also, you would get the element as the appropriate type rather than as Object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Randomize from dynamic array

    Also, though JM alluded to it, you should look into the Random object. I see you have Randomize in there, which suggests that you are using the old VB6 Rnd(). This has been replaced by the much easier Random object.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Randomize from dynamic array

    For a more complete code example using a Random object take a look at my CodeBank post regarding unique random selections.

    http://www.vbforums.com/showthread.php?t=393023
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Re: Randomize from dynamic array

    Ok I tried everything and still stumped. All I get is a "0" in the textbox. Don't know if i'm just tired or just don't get it, I'm confused as hell for sure. The result I want is a random list in the textbox of just the string only in two or three colums. ex:

    3A01 4C15 4D18
    2A10 3C17 1B45

    Code:
    Public Class frmMain
        Dim bunkname() As String
        Dim cells As Integer
    
        Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
            Dim prompt, title As String
            Dim i As Short
            prompt = "Enter each Bunk number."
            cells = InputBox("How many total bunks do you have?", "Create array")
            If cells > 0 Then ReDim bunkname(cells - 1)
            For i = 0 To UBound(bunkname)
                title = "Cells " & (i + 1)
                bunkname(i) = InputBox(prompt, title)
            Next
        End Sub
    
        Private Sub btnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click
            frmList.Show()
            Dim list As New ArrayList
            For i As Integer = 0 To UBound(bunkname)
                list.Add(i)
            Next i
    
            Dim item As Object
            Dim rand As New Random
    
            While list.Count > 0
                cells = rand.Next(0, list.Count)
                item = list(cells)
                list.RemoveAt(cells)
                frmList.tbxList.Text = (bunkname.ToString())
            End While
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width