]> git.kaiwu.me - klib.git/commitdiff
added table.shuffle()
authorHeng Li <lh3@live.co.uk>
Wed, 6 Apr 2011 16:19:14 +0000 (12:19 -0400)
committerHeng Li <lh3@live.co.uk>
Wed, 6 Apr 2011 16:19:14 +0000 (12:19 -0400)
lua/klib.lua

index 5421d4e9866feb8ce223977402014692439ee41a..ecb8bafb06b75093ecd6a5922ea177f0e0e968bf 100644 (file)
@@ -39,6 +39,7 @@
   string:split()
   io.xopen()
   table.ksmall()
+  table.shuffle()
   math.lgamma() >math.lbinom() >math.igamma()
   math.igamma() <math.lgamma() >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
 --