mirror of https://github.com/k3s-io/k3s
ShuffleStrings uses a seeded rand object
parent
891cef4efa
commit
da06ecfc1f
|
@ -54,3 +54,11 @@ func Seed(seed int64) {
|
|||
|
||||
rng.rand = rand.New(rand.NewSource(seed))
|
||||
}
|
||||
|
||||
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n)
|
||||
// from the default Source.
|
||||
func Perm(n int) []int {
|
||||
rng.Lock()
|
||||
defer rng.Unlock()
|
||||
return rng.rand.Perm(n)
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||
package rand
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -35,3 +36,17 @@ func TestString(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPerm(t *testing.T) {
|
||||
Seed(5)
|
||||
rand.Seed(5)
|
||||
for i := 1; i < 20; i++ {
|
||||
actual := Perm(i)
|
||||
expected := rand.Perm(i)
|
||||
for j := 0; j < i; j++ {
|
||||
if actual[j] != expected[j] {
|
||||
t.Errorf("Perm call result is unexpected")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ limitations under the License.
|
|||
package slice
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
utilrand "k8s.io/kubernetes/pkg/util/rand"
|
||||
"sort"
|
||||
)
|
||||
|
||||
|
@ -41,7 +41,7 @@ func SortStrings(s []string) []string {
|
|||
// order. It returns a new slice.
|
||||
func ShuffleStrings(s []string) []string {
|
||||
shuffled := make([]string, len(s))
|
||||
perm := rand.Perm(len(s))
|
||||
perm := utilrand.Perm(len(s))
|
||||
for i, j := range perm {
|
||||
shuffled[j] = s[i]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue