shuffling or randomizing a set of numbers.....

nynamyna

New member
Joined
Feb 24, 2010
Messages
2
Programming Experience
Beginner
Hi everybody

I have a set of nuumbers in a table in the access database. I just want to know how to shuffle those values randomly. Each time if I shuffle then the order of the numbers should be randomized. I know how to sort them in ascending and descending...but I don't know how to shuffle them....please help me.....

Thanks
Bharath
 

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
Couple of examples using a Shuffle(Of T) function and using LINQ to achieve the results.

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Using shuffle function
        Dim fruit As New List(Of String)
        fruit.AddRange(New String() {"Apple", "Banana", "Grape", "Orange", "Tomato"})
        fruit = Shuffle(Of String)(fruit.ToArray).ToList

        Dim ints As New List(Of Integer)
        ints.AddRange(New Integer() {1, 2, 3, 4, 5})
        ints = Shuffle(Of Integer)(ints.ToArray).ToList

        'Using lambda
        Dim fruit2 As New List(Of String)
        fruit2.AddRange(New String() {"Apple", "Banana", "Grape", "Orange", "Tomato"})
        Dim rnd As New Random()
        fruit2 = fruit2.OrderBy(Function() rnd.Next).ToList

        'Full LINQ syntax example
        Dim fruit3 As New List(Of String)
        fruit3.AddRange(New String() {"Apple", "Banana", "Grape", "Orange", "Tomato"})
        fruit3 = (From f In fruit3
                 Order By rnd.Next
                 Select f).ToList
    End Sub

    Private Shared Function Shuffle(Of T)(ByVal OriginalArray As T()) As T()
        Dim matrix = New SortedList()
        Dim r = New Random()

        For x = 0 To OriginalArray.Length - 1
            Dim i = r.Next()

            While matrix.ContainsKey(i)
                i = r.Next()
            End While

            matrix.Add(i, OriginalArray(x))
        Next

        Dim OutputArray = New T(OriginalArray.Length - 1) {}

        matrix.Values.CopyTo(OutputArray, 0)

        Return OutputArray
    End Function

End Class
 

picoflop

Well-known member
Joined
Feb 25, 2009
Messages
158
Programming Experience
5-10
@MattP: If your array is large, your function will run very long I assume (bc it selects a random number and checks if it was already selected). Fisher-Yates has a complexity of O(n), while yours is much worse for large n.
 

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
You're correct in that it would run slower for large sets of data. Rewrote it to use something more reasonable for large values of n.

VB.NET:
        Dim fruit As New List(Of String)
        fruit.AddRange(New String() {"Apple", "Banana", "Grape", "Orange", "Tomato"})
        ShuffleMe(Of String)(fruit)
VB.NET:
    Public Shared Sub ShuffleMe(Of T)(ByRef TheList As List(Of T))
        Dim rnd As New System.Random
        Dim tmpArray() As T = TheList.ToArray
        Dim len As Integer = tmpArray.Length
        While (len > 1)
            Dim idx As Integer = rnd.Next(len)
            len -= 1
            Dim temp As T = tmpArray(len)
            tmpArray(len) = tmpArray(idx)
            tmpArray(idx) = temp
        End While
        TheList = tmpArray.ToList
    End Sub
 

nynamyna

New member
Joined
Feb 24, 2010
Messages
2
Programming Experience
Beginner
thanks for your help.....I did it with your code....

Bharath
 
Top Bottom