Results 1 to 9 of 9

Thread: randomize a list

  1. #1

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    what is the fastest way to randomize a list, in your opinion? i have a few ideas but i just wanna know if someone has any really fast way of doing it

  2. #2
    Lively Member
    Join Date
    Mar 2000
    Posts
    81

    Question ?

    What do you mean by "randomize a list"? Are you saying you have a bunch of items in a list that you want re-ordered or an array that you want reordered or that you just want to select items randomly?

    Toot
    Some cause happiness wherever they go; others, whenever they go.

  3. #3
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    I suppose this depends on the size of the list. As with sort methods there are various ways of doing it, but the overhead (or efficiency) is directly related to the size of the collectiong being sorted.

    What are you trying to do, specifically and what size is the list?

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Thumbs up

    how about this ???

    Code:
    Public Sub Unsort(List() As Long, min As Long, max As Long)
    Dim i As Long
    Dim pos As Long
    Dim tmp As Long
    
        For i = min To max - 1
            pos = Int((max - i + 1) * Rnd + i)
            tmp = List(pos)
            List(pos) = List(i)
            List(i) = tmp
        Next i
    End Sub
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  5. #5
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    Its not inconceivable that the same random number occurs more than once. I would remove the item and place a NULL there. Then check for NULL prior to extracting ListItem. May then have to loop more than ListSize, but could increment counter as required.

    Otherwise an excellent response Paul282.

  6. #6

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    Originally posted by Steven McGarva
    Its not inconceivable that the same random number occurs more than once. I would remove the item and place a NULL there. Then check for NULL prior to extracting ListItem. May then have to loop more than ListSize, but could increment counter as required.

    Otherwise an excellent response Paul282.
    know what would kill you? having 30 nulls and one left sitting there...
    and the list could be anywhere from 5 to 5000 items, so maybe having multiple ways of randomizing it would be good

  7. #7

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    ok, i just tried my idea... it's the only one i can come up with that makes sense:

    having one unseen list, put whatever you want to randomize in the second list then going like this:

    ----

    Do
    x = Int(Rnd * List2.ListCount)
    List1.AddItem (List2.List(x))
    List2.RemoveItem (x)
    Loop Until List2.ListCount = 0

    ----


  8. #8
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Having multiple numbers pop up in the random number seed is not a big deal as it's swaping values sequentially (ie some numbers being swapped have already been moved once), it's also possible that after one swap a random number will reset a value to it's initial position... so what? it's random. If there is no chance of this happening then you're really just ordering to an obscure pattern.

    You didn't specify your objective just to 'randomize'. 'randomize' implies that some digits may stay in the same place

    I don't understand about the NULL arguement ????? you're always swapping a value not just moving it, try the function on a big list of numbers and see if any nulls or gaps appear.

    works sort of like a backward selection-sort

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  9. #9

    Thread Starter
    Addicted Member cwm's Avatar
    Join Date
    Mar 2000
    Posts
    133
    i just don't see how the topic "randomize a list" could be taken as anything but randomizing the possitions of list items... i.e. misc - sort - Randomize list in winamp.. at any rate, i figured out how to do it with just one list:

    Code:
    For x = 0 To List1.ListCount - 1
        y = Int(Rnd * (List1.ListCount - (x + 1))) + x - 1
        temp$ = List1.List(x)
        temp2$ = List1.List(y)
        List1.RemoveItem (x)
        List1.AddItem temp2$, x
        List1.RemoveItem (y)
        List1.AddItem temp$, y
    Next
    and i think that's what winamp does, because sometimes you can see the items in the playlist being replaced

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