From d7b0f2a524743fef990c6905e628304a0065116f Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Sat, 1 Dec 2012 14:11:46 -0800 Subject: [PATCH] math/rand: remove noop iteration in Perm The first iteration always do `m[0], m[0] = m[0], m[0]`, because `rand.Intn(1)` is 0. fun note: IIRC in TAOCP version of this algorithm, `i` goes backward (n-1->1), meaning that the "already" shuffled part of the array is never altered betweens iterations, while in the current implementation the "not-yet" shuffled part of the array is conserved between iterations. R=golang-dev CC=golang-dev https://golang.org/cl/6845121 --- src/pkg/math/rand/rand.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/math/rand/rand.go b/src/pkg/math/rand/rand.go index 94f84a85fb..ad2bf2fac0 100644 --- a/src/pkg/math/rand/rand.go +++ b/src/pkg/math/rand/rand.go @@ -100,7 +100,7 @@ func (r *Rand) Perm(n int) []int { for i := 0; i < n; i++ { m[i] = i } - for i := 0; i < n; i++ { + for i := 1; i < n; i++ { j := r.Intn(i + 1) m[i], m[j] = m[j], m[i] } -- 2.48.1