Everybody, your solution leaves something to be desired. What you have works but it has a terrible speed problem you can simply with same spirit do a linear algorithm. So while it works it is possibly very slow andit gets slower by each passing iteration of list (imagine trying to get the 10000th elemet in list to get randomized each missed step makes 9999 tests and dont even necceserily succeed)! But if we modify the idea bit it will be better.
OK so mel is a bit retartded in this sense. So your solutin makes SOME sense!
CODE
global proc randmizeListInPlaceInt(int $list[]){
$size=size($list);
for ($i=0;$i<$size-1;$i++){
$rnd = (int)floor(rand($size-$i))+$i;
$tmp = $list[$i];
$list[$i]=$list[$rnd];
$list[$rnd]=$tmp;
}
}
{
int $list[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
randmizeListInPlaceInt($list);
print $list;
}
Please note the list doesn't HAVE to go trough the whole list-1 its brobably allready very mixed by $list/1.2 or so but just the be sure the distribution is sane i use $size-1. But they are all O(n) so not to be nitpicky.
PS: So good coding is striving for either statistically fast solutions OR linear ones. Your might be statistically fast for short arrays but its very veru slow for long ones.