dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB7
Expertise: Intermediate
Oct 6, 2003

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


GetRandomArrayItem - Return a random item from an array or collection

' Return a random item from a mono/bi/tri-dimensional array or collection
' 
' Example:
'   Dim arr1 As String() = {"this is string 0", "this is string 1",
'  '       "this is string 2", "this is string 3", "this is string 4"}
'   Dim i As Integer
'   For i = 1 To 10
'       Debug.WriteLine(GetRandomArrayItem(arr1))
'   Next

'   Dim arr2 As String(,) = {{"this is string 0-0", "this is string 0-1"},
'  '       {"this is string 1-0", "this is string 1-1"},
'  '       {"this is string 2-0", "this is string 2-1"},
'  '       {"this is string 3-0", "this is string 3-1"},
'  '       {"this is string 4-0", "this is string 4-1"}}
'   For i = 1 To 10
'       Debug.WriteLine(GetRandomArrayItem(arr2))
'   Next
'
'   ' print the name of random controls on the form
'   For i = 1 To 10
'       Debug.WriteLine(DirectCast(GetRandomArrayItem(Me.Controls),
'  Control).Name)
'   Next

Function GetRandomArrayItem(ByVal coll As ICollection) As Object
    Static randGen As New System.Random
    ' if this is an array, convert to Array, and handle mono/bi/tri-dimensional 
    ' arrays
    If TypeOf coll Is Array Then
        Dim arr As Array = CType(coll, Array)
        If arr.Rank = 1 Then
            Dim minIndex0 As Integer = arr.GetLowerBound(0)
            Dim maxIndex0 As Integer = arr.GetUpperBound(0)
            Dim randomIndex0 As Integer = randGen.Next(minIndex0, maxIndex0 + 1)
            Return arr(randomIndex0)
        ElseIf arr.Rank = 2 Then
            Dim minIndex0 As Integer = arr.GetLowerBound(0)
            Dim maxIndex0 As Integer = arr.GetUpperBound(0)
            Dim randomIndex0 As Integer = randGen.Next(minIndex0, maxIndex0 + 1)
            Dim minIndex1 As Integer = arr.GetLowerBound(1)
            Dim maxIndex1 As Integer = arr.GetUpperBound(1)
            Dim randomIndex1 As Integer = randGen.Next(minIndex1, maxIndex1 + 1)
            Return arr(randomindex0, randomIndex1)
        ElseIf arr.Rank = 3 Then
            Dim minIndex0 As Integer = arr.GetLowerBound(0)
            Dim maxIndex0 As Integer = arr.GetUpperBound(0)
            Dim randomIndex0 As Integer = randGen.Next(minIndex0, maxIndex0 + 1)
            Dim minIndex1 As Integer = arr.GetLowerBound(1)
            Dim maxIndex1 As Integer = arr.GetUpperBound(1)
            Dim randomIndex1 As Integer = randGen.Next(minIndex1, maxIndex1 + 1)
            Dim minIndex2 As Integer = arr.GetLowerBound(2)
            Dim maxIndex2 As Integer = arr.GetUpperBound(2)
            Dim randomIndex2 As Integer = randGen.Next(minIndex2, maxIndex2 + 1)
            Return arr(randomindex0, randomIndex1, randomIndex2)
        End If
    Else
        ' otherwise get a random element from the collection
        Dim max As Integer = coll.Count
        Dim randomIndex As Integer = randGen.Next(0, max)
        Dim list As IList = CType(coll, IList)
        Return list.Item(randomIndex)
    End If
End Function
Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date