shuffle.go 381 B

1234567891011121314151617181920212223242526
  1. package shuffle
  2. import (
  3. "math/rand"
  4. "time"
  5. )
  6. func init() {
  7. rand.Seed(time.Now().UnixNano())
  8. }
  9. // Shuffler A type, typically a collection, that satisfies Shuffler
  10. // can be shuffle by Shuffle func
  11. type Shuffler interface {
  12. Len() int
  13. Swap(i, j int)
  14. }
  15. // Shuffle s
  16. func Shuffle(s Shuffler) {
  17. l := s.Len()
  18. for i := l; i > 0; i-- {
  19. j := rand.Intn(i)
  20. s.Swap(l-i, j)
  21. }
  22. }