Shuffle

  <Extension()>
  Function Shuffle(Of T)(ByRef items As IEnumerable(Of T), Optional iterations As Integer = 1, Optional seed As Integer = 0) As IEnumerable(Of T)

    Randomize()

    Dim r = If(seed = 0, New Random, New Random(seed))

    If iterations > 3 Then iterations = 3

    For i = 0 To iterations - 1
      items = From e In items Order By r.Next
    Next

    Return items

  End Function
Example:
    myList.Shuffle() 'Shuffles the List exactly one time. Already good enough for most cases.
    myList.Shuffle(2) 'Gives the List two shuffles.
    myList.Shuffle(100) 'Shuffles are limited to 3 times, even if more are specified. More shuffles are just not useful anymore.
    myList.Shuffle(, 1234) 'Uses 1234 as a seed value for the random number generator instead of .NETs default tickcount seed value.

Description

Shuffle-Method for randomizing IEnumerables (It's not the fastest method to do this, if that's important to you)

Details

Double click on the code to select all.

 

;