shuffle an array

Hello there,
is it possible to make this more random or something because its half working.. for example I never got "snake" any ideas?


Dim animals() As String = {"cat", "dog", "snake"}
Debug.Print(Shuffle_Random_From_Array(animals))


    Function Shuffle_Random_From_Array(ByRef data() As String, Optional ByVal MoreThanOne As Integer = 0) As String
        Dim Result As String = Nothing
        Dim Rand As New Random
        Dim daRandies As New ArrayList
        Dim i As Integer = 0

        For Each fern As String In data
            daRandies.Add(fern)
        Next

        For inx As Integer = daRandies.Count - 1 To 1 Step -1
            Dim position As Integer = Rand.[Next](inx)
            Dim temp As Object = daRandies(inx)
            daRandies(inx) = daRandies(position)
            daRandies(position) = temp
        Next

        If MoreThanOne > 0 Then
            For i = 1 To MoreThanOne
                Result += daRandies.Item(i - 1) & ","
            Next
            Result = Result.Substring(0, Result.Length - 1)
        Else
            Result = daRandies.Item(0)
        End If

        Return Result
    End Function

Open in new window

LVL 1
XK8ERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unknown_routineCommented:
Here is the issue:

For inx As Integer = daRandies.Count - 1 To 1 Step -1

Open in new window


daRandies.Count is 3  so this for only runs for inx=2 and inx=0


change it to:

For inx As Integer = daRandies.Count  To 1 Step -1

Open in new window

0
XK8ERAuthor Commented:
I get an error because there are 3 but 0,1,2 and not 1,2,3
0
unknown_routineCommented:
Ok here is the modifed version. I tested it and it retuerns all3 animals by random:

Imports System.Diagnostics

Module Module1

    Sub Main()
        Dim animals() As String = {"cat", "dog", "snake"}
        Debug.Write(Shuffle_Random_From_Array(animals))

    End Sub


    Function Shuffle_Random_From_Array(ByRef data() As String, Optional ByVal MoreThanOne As Integer = 0) As String
        Dim Result As String = Nothing
        Dim Rand As New Random
        Dim daRandies As New ArrayList
        Dim i As Integer = 0

        For Each fern As String In data
            daRandies.Add(fern)
        Next

        For inx As Integer = daRandies.Count - 1 To 0 Step -1
            Dim position As Integer = Rand.Next(0, 3)


            Dim temp As Object = daRandies(inx)
            daRandies(inx) = daRandies(position)
            daRandies(position) = temp
        Next

        If MoreThanOne > 0 Then
            For i = 1 To MoreThanOne
                Result += daRandies.Item(i - 1) & ","
            Next
            Result = Result.Substring(0, Result.Length - 1)
        Else
            Result = daRandies.Item(0)
        End If

        Return Result
    End Function


End Module

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.