VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Randomizes all items in a list box and puts the randomized list into a separate box.

by Dan Craggs (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Thu 3rd November 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Randomizes all items in a list box and puts the randomized list into a separate box.

Rate Randomizes all items in a list box and puts the randomized list into a separate box.



' listboxes, named 'Openlist', 'Donelist' and 'Templist' (minus
' quotes. There's probably a better way to do this, but this is just what
' I came up with. It's simple really, but hopefully it should help someone.
' Comments or suggestions, contact me via the contact form on PSC. If you
' want to use this with an array, just modify the adding, deleting and
' counting actions for whatever you need. You'll need a bit of Rediming in
' there too.

' No warranty is provided for this script or its actions, neither expressed
' nor implied. No liability is assumed for the use, misuse, or inability to
' use this script, or the consequences thus.

Dim fromline As Double
Dim thisixloc As Double
' Names used here:
' Openlist  - this is a listbox containing all the items you want to
'             randomize. Where I've used it, it's contained lines opened
'             from a text file, hence 'Openlist' - Just replace all if
'             you really must change it
' Donelist  - This is the listbox which will contain the finished product
'             when it's all been randomized
' Templist  - The temporary store for the latter part of the Donelist (more
'             later)
' thisixloc - The current index number a new item should take (more later)
' fromline  - The current line index we're dealing with

' Add the first item to the donelist.
' It doesn't matter what item is added first, since by the end it could be
' anywhere in the list
Donelist.AddItem (Openlist.List(0))
' We're going to do this process for every item in the Openlist, otherwise
' we'd be leaving things out. Common sense really
For Item = 1 To Openlist.ListCount - 1
    ' Generate a new random seed. Make all this randomness a little more
    ' random
    Randomize
    ' This generated an index location number, i.e. after this process has
    ' finished, this is the index number we want it to end up as. Bear in
    ' mind we're working in Base 0 here, so we start from 0. Also, the new
    ' item could go at the end of the list. So that would be the highest
    ' index value plus 1, which is the same as the ListCount, so we'll use
    ' that instead. The formula here is:
    ' Int((Highest - Lowest + 1) * Rnd + Lowest)
    ' Just generates us a number between our upper and lower bounds. Note
    ' that because we have 0 as a lower bound, adding or subtracting it does
    ' nothing, so the -0 and +0 just disappear to leave us with the
    ' following:
    thisixloc = Int((Donelist.ListCount + 1) * Rnd)
    ' If the generated index number is 0, it means the current item is gonna
    ' be put at the start of the list.
    If thisixloc = 0 Then
    ' So, we clear the temp store (here Templist) just in case, for whatever
    ' reason, there's something left over in there...
        Templist.Clear
        ' ...move everything in Donelist over to the temporary store...
        For fromline = 0 To Donelist.ListCount - 1
            Templist.AddItem (Donelist.List(fromline))
        Next fromline
        ' ...clear the donelist...
        Donelist.Clear
        ' ...add the item...
        Donelist.AddItem (Openlist.List(Item))
        ' ...move all the items back after the new item...
        For fromline = 0 To Templist.ListCount - 1
            Donelist.AddItem (Templist.List(fromline))
        Next fromline
        ' ...and finally clear the temp store so it's fresh for next time
        ' it's used
        Templist.Clear
    ' If our index number is one higher than the highest index in the
    ' donelist, this means we want to put the item on the end of the list
    ElseIf thisixloc = Donelist.ListCount Then
    ' So of course, we just add it straight on there
        Donelist.AddItem (Openlist.List(Item))
    ' Any other index number means we're gonna have to slip it into the
    ' list, in between what's already there. So, we'll split the list
    ' in two. The first part will be everything up to where we want the
    ' item to go, and the second part will be everything after it. So,
    ' we'll move the second part to the temporary store, remove the items
    ' that we just transferred, add the new item to the end of the first
    ' part of the list, then transferring everything back from the temp
    ' store, sticking it back on the end of our list.
    ' EDIT: Originally what I did was move the first part to one store,
    '       then the second to another store. Re-add the first one, then
    '       the item, then the second one. However, this meant that in the
    '       computer memory, the list had to be duplicated, taking up both
    '       memory itself and time. Especially for long lists, duplicating
    '       a large portion of the list when it's not required was a silly
    '       thing to be doing. So now, hopefully, if we just delete things
    '       off the end, the first part can stay exactly where it was in
    '       the memory, and we just add to it.
    Else
        ' Clear the temporary store, again in case there's something left
        ' there for whatever reason
        Templist.Clear
        ' Now, we'll take the second part of the list (from the index
        ' location right to the end) and put it in the temp store.
        ' If I type 'story' instead of 'store' one more time I'll go nuts
        For fromline = thisixloc To Donelist.ListCount - 1
            Templist.AddItem (Donelist.List(fromline))
        Next fromline
        ' Now, we'll remove exactly the same items. We couldn't really
        ' remove them in the For Next loop above or we'd end up messing
        ' up our index numbers and making the process one helluva lot more
        ' complex than it really needs to be. Note that as the number of
        ' items in the list decreases as we delete them, we can just keep
        ' deleting whatever's in the current index location. If we do this
        ' the number of times there are things to delete, then all of them
        ' will go
        For fromline = thisixloc To Donelist.ListCount - 1
            Donelist.RemoveItem (thisixloc)
        Next fromline
        ' Here comes our new item
        Donelist.AddItem (Openlist.List(Item))
        ' Then move everything back onto the end of that list...
        For fromline = 0 To Templist.ListCount - 1
            Donelist.AddItem (Templist.List(fromline))
        Next fromline
        ' ...and clear the temp store again
        Templist.Clear
    End If
Next Item

Download this snippet    Add to My Saved Code

Randomizes all items in a list box and puts the randomized list into a separate box. Comments

No comments have been posted about Randomizes all items in a list box and puts the randomized list into a separate box.. Why not be the first to post a comment about Randomizes all items in a list box and puts the randomized list into a separate box..

Post your comment

Subject:
Message:
0/1000 characters