Random table sort

Hi, I have a table that I want to sort randomly,

I have this code:


function randomSort(a,b)
if(math.random(10) < 5) then return true end
return false
end

table.sort(myTable, randomSort)

But it sometimes crashes (not allways) saing that randomSort is not a valid sort function…

Any ideas on how to solve this?

Thanks [import]uid: 63561 topic_id: 12003 reply_id: 312003[/import]

I have the same problem with unsort table

 table.sort(parejas, function(a, b) return math.Rand( 0, 1 ) \> 0.5 end);

I’ve solved with this code. it isn’t elegant, but it works.

  
 --semilla para iniciar el ramdom de botellas  
 local semilla = math.randomseed  
 local generarRandom = math.random  
 --randon siempre genera la misma secuencia numérica si no usamos esta función.  
 semilla( os.time() + 1 )  
  
 local parejas = {"card1", "card2", "card3", "card4", "card5", "card6", "card7", "card8",  
 "card1", "card2", "card3", "card4", "card5", "card6", "card7", "card8"}  
  
 --Unsort function  
 function desordenarTabla(tempArray)  
 local i, indiceAletorio  
 --array unsort  
 local auxArray = {}  
 --Total of item  
 local TotalItem = 16  
  
 --unsort array  
 for i = 1, 16 do  
 indiceAletorio = generarRandom(TotalItem)  
 table.insert(auxArray, parejas[indiceAletorio])  
 table.remove (parejas, indiceAletorio)  
 TotalItem = TotalItem - 1  
 end  
  
 return auxArray  
 end  
  
 parejas = desordenarTabla(parejas)  

[import]uid: 55048 topic_id: 12003 reply_id: 44755[/import]