Lua: added new helper.shuffle() function
This commit is contained in:
parent
3397a38cff
commit
aab7c8afcf
2 changed files with 12 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue