cloudaice / learnGo

there are something about Go Useage
MIT License
0 stars 0 forks source link

精彩的实现随机数组 #18

Open cloudaice opened 8 years ago

cloudaice commented 8 years ago
   145  // Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
   146  func (r *Rand) Perm(n int) []int {
   147      m := make([]int, n)
   148      // In the following loop, the iteration when i=0 always swaps m[0] with m[0].
   149      // A change to remove this useless iteration is to assign 1 to i in the init
   150      // statement. But Perm also effects r. Making this change will affect
   151      // the final state of r. So this change can't be made for compatibility
   152      // reasons for Go 1.
   153      for i := 0; i < n; i++ {
   154          j := r.Intn(i + 1)
   155          m[i] = m[j]
   156          m[j] = i
   157      }
   158      return m
   159  }