GetRandomArrayItem – Return a random item from an array or collection

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)'   NextFunction 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 IfEnd Function

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as