Lua: added new helper.shuffle() function

This commit is contained in:
Elvish_Hunter 2013-01-03 13:22:15 +00:00
parent 3397a38cff
commit aab7c8afcf
2 changed files with 12 additions and 0 deletions

View file

@ -27,6 +27,7 @@ Version 1.11.1+svn:
Portuguese (Brazil)
* Lua API:
* new wesnoth.get_time_stamp() function
* new helper.shuffle() function
* Multiplayer:
* Moved new lobby option in Preferences -> Multiplayer to Advanced
Preferences and clarified description

View file

@ -380,4 +380,15 @@ function helper.round( number )
return number
end
function helper.shuffle( t )
-- since tables are passed by reference, this is an in-place shuffle
-- it uses the Fisher-Yates algorithm, also known as Knuth shuffle
assert( type( t ) == "table", string.format( "helper.shuffle expects a table as parameter, got %s instead", type( t ) ) )
local length = #t
for index, value in ipairs( t ) do
local random = math.random( 1, length )
t[index], t[random] = t[random], t[index]
end
end
return helper