工作上遇到一个问题,好奇goalng的排列数Perm是怎么实现的,看了下源代码,写的很简洁。

使用了随机交换算法来得到一个排列组合。

1
2
3
4
5
package rand // import "math/rand"

func Perm(n int) []int
    Perm returns, as a slice of n ints, a pseudo-random permutation of the
    integers in the half-open interval [0,n) from the default Source.

交换

本质上说就是交换m[i]m[j],且i> j

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
func Perm(n int) []int {
	m := make([]int, n)
	for i := 0; i < n; i++ {
		j := rand.Intn(i + 1)
        // std implement
		// m[i] = m[j]
		// m[j] = i

		// swap
		m[i], m[j] = m[j], i

        // same effect
		// m[i] = i
		// m[i], m[j] = m[j], m[i]
	}
	return m
}