生成一定数量后的随机循环

时间:2014-03-15 21:36:10

标签: .net vb.net random

我试图以随机顺序播放歌曲,播放完所有歌曲后,全部变为nextMusic.WasPlayed = False并且随机数生成器继续生成数字,但在它再次播放了一些歌曲后,生成器进入循环并不停地生成数字

     Dim RanTrack As Random = New Random(TimeOfDay.Millisecond)
     Private Sub nextBTN_Click(sender As Object, e As EventArgs) Handles nextBTN.Click
        If My.Settings.Shufle = True Then
  generateNumber:
            Dim trackN As Integer = RanTrack.Next(musicControl.Controls.Count - 1)
                Debug.WriteLine("number " & trackN)
                Dim nextMusic As MusicItem = musicControl.Controls.Item(trackN)
                If nextMusic.WasPlayed = False Then
                    nextMusic.PlayMusic()
                Else
                    GoTo generateNumber
                End If
         End If
      End Sub

1 个答案:

答案 0 :(得分:0)

尝试做这样的事情:

Dim RanTrack As Random = New Random()
Private Sub nextBTN_Click(sender As Object, e As EventArgs) Handles nextBTN.Click
    If My.Settings.Shufle = True Then
        Dim nextMusics = ( _
            From nextMusic In musicControl.Controls.Cast(Of MusicItem)() _
            Where nextMusic.WasPlayed = False _
            Order By RanTrack.NextDouble() _
            Select nextMusic).ToArray()
        If nextMusics.Any() Then
            nextMusics(0).PlayMusic()
        End If
    End If
End Sub