Skip to content

Commit fb01ad2

Browse files
yaojingguorandall77
authored andcommitted
math/rand: add a comment for the i=0 iteration
Fixes #13215 Change-Id: I126117d42e7c1e69cbc7fad0760e225b03ed15bd Reviewed-on: https://go-review.googlesource.com/16852 Reviewed-by: Keith Randall <[email protected]>
1 parent 7af0839 commit fb01ad2

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/math/rand/rand.go

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ func (r *Rand) Float32() float32 {
148148
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
149149
func (r *Rand) Perm(n int) []int {
150150
m := make([]int, n)
151+
// In the following loop, the iteration when i=0 always swaps m[0] with m[0].
152+
// A change to remove this useless iteration is to assign 1 to i in the init
153+
// statement. But Perm also effects r. Making this change will affect
154+
// the final state of r. So this change can't be made for compatibility
155+
// reasons for Go 1.
151156
for i := 0; i < n; i++ {
152157
j := r.Intn(i + 1)
153158
m[i] = m[j]

0 commit comments

Comments
 (0)