aboutsummaryrefslogtreecommitdiffstats
path: root/utils.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2016-05-05 23:24:44 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2016-05-05 23:24:44 -0400
commit2fdccd98d65b03f6550d1824a7c5541cb1d175cf (patch)
treef393245d555923813d0c44e09e3f59c90267b05d /utils.go
parentbe4c150fe17c4869e80acd2a18a380aa91c49d65 (diff)
downloadpos-2fdccd98d65b03f6550d1824a7c5541cb1d175cf.tar.gz
Add Fisher-Yates schuffle
Diffstat (limited to 'utils.go')
-rw-r--r--utils.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/utils.go b/utils.go
index 513786c..fe17650 100644
--- a/utils.go
+++ b/utils.go
@@ -1,5 +1,10 @@
package main
+import (
+ "math/rand"
+ "sort"
+)
+
// floor of log(x) (i.e MSB for positive integers)
func Log(x int64) int64 {
var r int64 = 0
@@ -34,3 +39,11 @@ func Post(size int64, height int64, id int64) int64 {
}
return r
}
+
+func Shuffle(data sort.Interface) {
+ var j int
+ for i := data.Len() - 1; i > 0; i-- {
+ j = rand.Intn(i + 1)
+ data.Swap(i, j)
+ }
+}