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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 --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)
views:791 update:2011/9/28 21:38:26
corona forums © 2003-2011