-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshuffle.go
More file actions
39 lines (30 loc) · 753 Bytes
/
shuffle.go
File metadata and controls
39 lines (30 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2019 Kazuhisa TAKEI<xtakei@rytr.jp>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package loncha
import (
"math/rand"
"reflect"
"github.com/seehuhn/mt19937"
)
// Shuffle is shuffing slice order. if slice is not pointer of slice or not slice, return error
func Shuffle(slice interface{}, seeds ...int64) (e error) {
rv, err := sliceElm2Reflect(slice)
if err != nil {
return err
}
length := rv.Len()
if length == 0 {
return
}
randMt := rand.New(mt19937.New())
if len(seeds) > 0 {
randMt.Seed(seeds[0])
}
swap := reflect.Swapper(rv.Interface())
for i := 0; i < length; i++ {
r := i + randMt.Intn(length-i)
swap(r, i)
}
return
}