bilibili-backup/app/service/main/bns/lib/shuffle/shuffle.go
2019-04-22 02:59:20 +00:00

27 lines
381 B
Go

package shuffle
import (
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Shuffler A type, typically a collection, that satisfies Shuffler
// can be shuffle by Shuffle func
type Shuffler interface {
Len() int
Swap(i, j int)
}
// Shuffle s
func Shuffle(s Shuffler) {
l := s.Len()
for i := l; i > 0; i-- {
j := rand.Intn(i)
s.Swap(l-i, j)
}
}