Running the code and looking over the output, it seems the odd shuffling results come from the poor random number generator* (1st “random” number generated is always ‘1’) and also because each time through the loop your lowering the size of the random number pool (1st loop, pool of 10 numbers; 2nd loop, pool of 9 numbers; and so on).
A shuffling method which I’ve used is simply to loop through the list of numbers a few times, picking two indexes at random and swapping their values.
Here’s some code:
--- code start ---
local function shuffle( a )
local c = #a
math.randomseed( system.getTimer() )
for i = 1, (c \* 20) do
local ndx0 = math.random( 1, c )
local ndx1 = math.random( 1, c )
local temp = a[ndx0]
a[ndx0] = a[ndx1]
a[ndx1] = temp
end
return a
end
--- code end ---
It’s simple, short and quick. The sample loops through the table 20 times (which takes about a split second), but a smaller number can be used and still get good results.
HTH
*From the docs: “This function is an interface to the simple pseudo-random generator function rand provided
by ANSI C. (No guarantees can be given for its statistical properties.)” Nuff said. [import]uid: 1581 topic_id: 313 reply_id: 463[/import]