Skip to content

a iteration can be saved for "func (r *Rand) Perm(n int) []int" #13215

Closed
@yaojingguo

Description

@yaojingguo

In src/math/rand/rand.go, there is the following code:

func (r *Rand) Perm(n int) []int {
    m := make([]int, n)
    for i := 0; i < n; i++ {
        j := r.Intn(i + 1)
        m[i] = m[j]
        m[j] = i
    }
    return m
}

i can start from 1 instead of 0 since the iteration for i=0 always swap m[0] with m[0]. A fix is to change i := 0 to i := 1.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions