ID:2256449
 
(See the best response by Ter13.)
Code:


Problem description:
I'm sure I could figure this out one, if I had the time, but I don't have much of that these days. What would the best way to reorder a list randomly while keeping all the items be?
Best response
The "best way" to me is one that is memory stable. In other words, it doesn't generate a second list to perform the shuffle.

var/len = list.len, pos, pos2
for(pos in 1 to len) //select index1 in sequence
pos2 = rand(1,len) //pick index2 at random
if(pos2!=pos) //if they aren't the same
list.Swap(pos,pos2)


This swapping method may shuffle multiple indices multiple times, but it will ensure that each item is moved at least once.

Also note that if list is passed as an instance member, you should store it as a local variable for the duration of the sort. This removes unneeded lookups. If it's passed as an argument, it's fine too, as arguments are just special local variables.
Given that list.Swap() is twice as fast? No.
Thank you for the advice Ter, I never even stopped to think of storing a list length as a local var. I'm sure this will be very helpful in speeding up my procs, because I had used length(list) alot of times in loops already.

The only thing I don't fully understand is if by random chance the pos2 is the same as the pos, and that item does not get swapped, will it be checked again until it is swapped? Would I have to use a while loop, and a counter to make sure every item is moved, or would that not be feasable?
It's a waste of processing power to ensure that all items have been moved, because as the number of items left to be swapped decreases, the number of places to put them also decreases.

Also, it's less random.

Basically, I don't think it's really a problem.