Randomize a list?

yogi_bear_79

Member
Joined
Sep 30, 2009
Messages
19
Programming Experience
Beginner
I have a list of names:

VB.NET:
Public players As List(Of Player) = New List(Of Player)

How can I randomize the list?

These are names of players in a tournament. I want to randomize the list so that they are not sitting sequentially at the table(s)
 
Randomising a List is dead easy in .NET 3.5 thanks to LINQ:
VB.NET:
myList = (From item In myList _
          Let rng = New Random _
          Order By rng.NextDouble() _
          Select item).ToList()
That will work no matter the type of the List items.
 
I tried various placements of the code, nothing errors out at build or runtime, but the list is not randomized

VB.NET:
Public Sub arrange_players()

        players = (From item In players Let rng = New Random Order By rng.NextDouble() Select item).ToList()
        
        Dim i As Integer

        For x = 1 To Math.Ceiling(NumPlayers / NumPerTable)
            For y = 1 To NumPerTable
                table(x).Items.Add(players(i).Name)
                i = i + 1
            Next
        Next

    End Sub
 
Back
Top