ExtensionMethod.NET Home of 846 C#, Visual Basic, F# and Javascript extension methods


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


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


    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

    Return items

  End Function


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.

Author: noxon

Submitted on: 5 mrt 2013

Language: VB

Type: System.Collection.Generic.IEnumerable<T>

Views: 3673