1
Dim myRandom As New Random
Dim myList As New List(Of String)(New String() {"A", "B", "C"})
myList.OrderBy(Function(i) myRandom.Next).ToList()
For k As Integer = 0 To 2
    MessageBox.Show(myList.Item(k))
Next

When you run the code, you will see that the MessageBoxes show A,B,C.

I want MessageBoxes show B,C,A or C,B,A or A,C,B or A,B,C or B,A,C or C,A,B according to randomized result.

Note: Using Linq is a must.

  • 1
    [https://stackoverflow.com/questions/108819/best-way-to-randomize-an-array-with-net](https://stackoverflow.com/questions/108819/best-way-to-randomize-an-array-with-net) – mahlatse Nov 22 '18 at 09:40

1 Answers1

4

The issue is that your code doesn't make any changes to myList. A LINQ query ALWAYS generates a new list. You need to assign the result of ToList back to your myList variable, i.e.

myList = myList.OrderBy(Function(i) myRandom.Next).ToList()
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46