]> Cypherpunks repositories - gostls13.git/commitdiff
math/rand: remove noop iteration in Perm
authorJohan Euphrosine <proppy@google.com>
Sat, 1 Dec 2012 22:11:46 +0000 (14:11 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 1 Dec 2012 22:11:46 +0000 (14:11 -0800)
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

index 94f84a85fbe14a0f2e1f8d62a6ca1d3fc546dc93..ad2bf2fac0865d63f504e4bcb5e9733c9fe8e7a0 100644 (file)
@@ -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]
        }