Commit d7b0f2a5 authored by Johan Euphrosine's avatar Johan Euphrosine Committed by Brad Fitzpatrick

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
parent 444b7b53
......@@ -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]
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment