You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
381 B
26 lines
381 B
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) |
|
} |
|
}
|
|
|