|
-
Apr 13th, 2007, 08:22 PM
#1
Thread Starter
Lively Member
[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
-
Apr 13th, 2007, 08:55 PM
#2
Re: Randomize from dynamic array
You get a random element from an array the same way regardless of how it was created:
vb Code:
Dim index As Integer = myRandom.Next(0, myArray.Length)
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.
-
Apr 13th, 2007, 09:12 PM
#3
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
 
-
Apr 13th, 2007, 09:27 PM
#4
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
-
Apr 14th, 2007, 02:42 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|