Results 1 to 8 of 8

Thread: Shuffle an Array

  1. #1

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616

    Shuffle an Array

    I have an array that I want to shuffle. By shuffle I mean move the elements around to different locations.

    Does anyone know of a simple way to make this happen? I did a search on "random array" and a search on "shuffle array" but neither turned up the results I had hoped for.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Its probably easier to retrieve them randomly then to move them around inside the array to a random order.

  3. #3

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    cant it has to be programmed in.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Did you have a look at Reverse() Function in the array obj ? I don't know if that's what want .

  5. #5

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Doesn't reverse just reverse the order?? that isnt shuffling the order.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Do something like this (psudocode):
    Code:
    For i=0 to array.length
    
    ' get a random number between 0 and array.length
    Dim Random as ......a random number code.
    
    Dim Temp as String
    
    Temp = array(i)
    
    array(i) = array(Random)
    
    array(Random) = Temp
    
    Next
    So basically, you go through each item, and randomly switch it with another item.

  7. #7
    Lively Member
    Join Date
    May 2002
    Posts
    94
    I just threw this together... Let me know how it works...

    place code inside a button
    Code:
    Dim items() As String = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}
    ShuffleArray(items)
    Module
    Code:
      Public Function ShuffleArray(ByRef Obj())
        Dim xCount As Integer = 0
        Dim TmpArray()
        ReDim TmpArray(0)
        Dim xVal As Integer
    
        Randomize()
        Do Until xCount = Obj.Length
          xVal = CInt(Int(Obj.Length * Rnd()))
          If Not Obj(xVal) Is Nothing Then
            If xCount > 0 Then ReDim Preserve TmpArray(xCount)
            TmpArray(xCount) = Obj(xVal)
            xCount += 1
            Array.Clear(Obj, xVal, 1)
          End If
        Loop
    
        Dim i As Integer = 0
        For i = 0 To xCount - 1
          Obj(i) = TmpArray(i)
        Next
    
      End Function

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    There is no need for all that. I took my psudocode and wrote it out for you.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim items() As String = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}
    3.         items = ShuffleArray(items)
    4.     End Sub
    5.  
    6.     Private Function ShuffleArray(ByVal obj() As Object) As Object()
    7.         Dim Temp As Object
    8.         Dim randomNum As Integer
    9.         Dim i As Integer
    10.         For i = 0 To obj.Length - 1
    11.             randomNum = New System.Random().Next(1, obj.Length - 1)
    12.             Temp = obj(i)
    13.             obj(i) = obj(randomNum)
    14.             obj(randomNum) = Temp
    15.         Next
    16.         ShuffleArray = obj
    17.     End Function
    Last edited by hellswraith; Sep 13th, 2003 at 02:01 PM.

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