Results 1 to 12 of 12

Thread: (.NET 3.5) Randomise a List of Items

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    (.NET 3.5) Randomise a List of Items

    C# version here.

    The following is an extension method for getting the contents of any IEnumerable(Of T) object, e.g. an array or List(Of T), in random order:
    Code:
    Imports System.Runtime.CompilerServices
    
    Public Module Enumerable
    
        <Extension()> _
        Public Function Randomise(Of T)(ByVal items As IEnumerable(Of T)) As IEnumerable(Of T)
            Dim result As T() = Nothing
    
            If items IsNot Nothing Then
                Dim rng As New Random
    
                result = (From item In items Order By rng.NextDouble()).ToArray()
            End If
    
            Return result
        End Function
    
    End Module
    Note that it obeys the rules for extension methods, i.e. it's a member of a module, is decorated with the Extension attribute and takes the target type as the first argument. You might use it something like this:
    vb.net Code:
    1. Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    2.  
    3. For Each number In numbers.Randomise()
    4.     MessageBox.Show(number.ToString())
    5. Next
    Try running that repeatedly and you'll get a different order each time.

    Note that you could write a regular method that does the same thing in earlier versions.

    EDIT: It's been brought to my attention that there is a significant issue with this code that, while unlikely to be encountered, could cause the operation to fail. Here's a reimplementation that isn't susceptible to this issue:
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module Enumerable
    4.  
    5.     <Extension>
    6.     Public Function Randomise(Of T)(ByVal items As IEnumerable(Of T)) As IEnumerable(Of T)
    7.         Dim result As T() = Nothing
    8.  
    9.         If items IsNot Nothing Then
    10.             Dim rng As New Random
    11.  
    12.             result = items.ToArray()
    13.  
    14.             'Generate one random value per item.
    15.             Dim keys = Array.ConvertAll(result, Function(item) rng.NextDouble())
    16.  
    17.             'Sort the items by their corresponding random keys, thus randomising them.
    18.             Array.Sort(keys, result)
    19.         End If
    20.  
    21.         Return result
    22.     End Function
    23.  
    24. End Module
    Last edited by jmcilhinney; May 18th, 2017 at 01:02 AM. Reason: Simplified code
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width