1
0
mirror of https://github.com/golang/go synced 2024-11-12 09:50:21 -07:00

random permutation function

func perm(n int) *map[int]int

R=r
OCL=19340
CL=19340
This commit is contained in:
Ken Thompson 2008-11-16 13:02:47 -08:00
parent 9c7b064043
commit 2567c073ea

View File

@ -14,6 +14,7 @@ package rand
// urand32 - return random uint32 // urand32 - return random uint32
// nrand, nrand31, nrand63 - return 0 <= random < n // nrand, nrand31, nrand63 - return 0 <= random < n
// frand, frand64, frand32 - return 0 <= random float, float64, float32 < 1 // frand, frand64, frand32 - return 0 <= random float, float64, float32 < 1
// perm gives a random permutation map[int]int
const const
( (
@ -162,6 +163,22 @@ frand() float
return float(frand64()) 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 func
init() init()
{ {