Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomly sort a collection/array

Status
Not open for further replies.

DrBowes

Programmer
Jul 16, 2003
150
GB

Would anybody know a slick way to sort a collection or array into a random order?

The only method I can think of at the moment is to put the values into a worksheet, assign random numbers to the next column, sort and reload into the collection/array. Yuk
 
Surely sorting into a random order is a contradiction in terms.

However, you could create a new array with the same dimensions then use random index selections to fill that array.
 
I would think that it would be better to simply randomize the entry retrieval order rather than to actually try to sort the data set.

Use the Randomize function to see the Random number generator, then use the Rnd function to obtain a random value. If you're using an array, then use the LBound(Array) and UBound(Array) functions to put the value in range, and for a collection use 1 and Collection.Count to set the range. Use that random number then as the index of the next value to retrieve. If you can only choose each entry once, then a collection works very well because you can remove the item from the original collection and add it to another collection once selected, and you don't have to keep another data structure somewhere to keep track of what you've already selected. You second collection then still has all of the values.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top