summaryrefslogtreecommitdiff
path: root/src/lib/rand.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/rand.go')
-rw-r--r--src/lib/rand.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/lib/rand.go b/src/lib/rand.go
index 8619cc48c..ef122d4a9 100644
--- a/src/lib/rand.go
+++ b/src/lib/rand.go
@@ -14,6 +14,7 @@ package rand
// urand32 - return random uint32
// nrand, nrand31, nrand63 - return 0 <= random < n
// frand, frand64, frand32 - return 0 <= random float, float64, float32 < 1
+// perm gives a random permutation map[int]int
const
(
@@ -162,6 +163,22 @@ frand() float
return float(frand64())
}
+export func
+perm(n int) *map[int]int
+{
+ m := new(map[int]int);
+ for i:=0; i<n; i++ {
+ m[i] = i;
+ }
+ for i:=0; i<n; i++ {
+ j := nrand(n);
+ t := m[i];
+ m[i] = m[j];
+ m[j] = t;
+ }
+ return m;
+}
+
func
init()
{