EDIT: I am so sorry! I have added my thread to the C# discussion thread instinctively wherein in fact I have been creating my application in VB.NET so sorry! Please transfer my thread to the appropriate location! Really sorry!

Hey all I am currently trying to mess around with a simple mp3 player and am in the process of creating a shuffle list from all the songs in my playlist.

So here is my random number generating code (which I guess does not work for it hangs my application)

For i As Integer = 0 To Me.songList.Count - 1
                Dim wasIndexAdded As Boolean = False
                Do Until wasIndexAdded
                    Dim randomClass As New Random(Me.songList.Count - 1)
                    Dim randNum As Integer = -1
                    randNum = randomClass.Next()
                    If Not Me.shuffleIndexList.Contains(randNum) Then
                        Me.shuffleIndexList.Add(randNum)
                        wasIndexAdded = True
                    End If
                Loop
            Next i

I do not know what is wrong with the code, I have tried another version of that here:

For i As Integer = 0 To Me.songList.Count - 1
                Dim wasIndexAdded As Boolean = False
                Dim randomClass As New Random()
                Do Until wasIndexAdded
                    Dim randNum As Integer = -1
                    randNum = randomClass.Next(Me.songList.Count - 1)
                    If Not Me.shuffleIndexList.Contains(randNum) Then
                        Me.shuffleIndexList.Add(randNum)
                        wasIndexAdded = True
                    End If
                Loop
            Next i

Any help or suggestions will be greatly appreciated!

Recommended Answers

All 2 Replies

Try this,

Sub Main()
        Dim count As Integer = Me.songList.Count
        Dim lst As New List(Of Integer)
        For i As Integer = 1 To count
            lst.Add(i)
        Next

        Dim rnd As New Random
        Dim ind1, ind2, tmp As Integer
        For i = 1 To count
            ind1 = rnd.Next(count)
            ind2 = rnd.Next(count)

            tmp = lst(ind1)
            lst(ind1) = lst(ind2)
            lst(ind2) = tmp
        Next


        For Each t As Integer In lst
            Console.WriteLine(t)
        Next
    End Sub

Oh you used that swapping algorithm here right? where in you randomize one index in the list, get the value of that index, store it in temp, assign a new value to the index by getting a value from another randomized index and then storing the previous index's value to the new randomized index's value, is this right? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.