summaryrefslogtreecommitdiff
path: root/src/lib/rand.go
diff options
context:
space:
mode:
authorKen Thompson <ken@golang.org>2008-11-16 13:02:47 -0800
committerKen Thompson <ken@golang.org>2008-11-16 13:02:47 -0800
commite2e6ab6ff08ef8794756102e3ec3f3d5a3de2286 (patch)
treeb3b9bd1d3b93817f07e0369355e5b01ea2b72454 /src/lib/rand.go
parentccf1cbd0569299bd031f6856f7fcbe91e31267c4 (diff)
downloadgolang-e2e6ab6ff08ef8794756102e3ec3f3d5a3de2286.tar.gz
random permutation function
func perm(n int) *map[int]int R=r OCL=19340 CL=19340
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()
{