PHP » GoLang |
login |
register |
about
|
shuffleshuffle[code] func Shuffle(source interface{}) { valueOf := reflect.ValueOf(source) if valueOf.Type().Kind() != reflect.Slice { return } length := valueOf.Len() if length < 2 { return } rand.Seed(time.Now().UnixNano()) swapper := reflect.Swapper(source) for i := 0; i < length; i++ { j := rand.Intn(length) swapper(i, j) } } [/code] |