From: Heng Li Date: Wed, 6 Apr 2011 16:19:14 +0000 (-0400) Subject: added table.shuffle() X-Git-Tag: ksprintf-final~58 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=54004e39cf19a14254b45208d8e330de67c782b1;p=klib.git added table.shuffle() --- diff --git a/lua/klib.lua b/lua/klib.lua index 5421d4e..ecb8baf 100644 --- a/lua/klib.lua +++ b/lua/klib.lua @@ -39,6 +39,7 @@ string:split() io.xopen() table.ksmall() + table.shuffle() math.lgamma() >math.lbinom() >math.igamma() math.igamma() matrix.chi2() math.erfc() @@ -163,6 +164,13 @@ function table.ksmall(arr, k) end end +function table.shuffle(a) + for i = #a, 1, -1 do + local j = math.random(i) + a[j], a[i] = a[i], a[j] + end +end + -- -- Mathematics --