Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
3758a01c
Commit
3758a01c
authored
Sep 11, 2015
by
Mike Danese
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13755 from ArtfulCoder/shufflestrings
ShuffleStrings uses a seeded rand object
parents
a4932454
da06ecfc
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
2 deletions
+25
-2
rand.go
pkg/util/rand/rand.go
+8
-0
rand_test.go
pkg/util/rand/rand_test.go
+15
-0
slice.go
pkg/util/slice/slice.go
+2
-2
No files found.
pkg/util/rand/rand.go
View file @
3758a01c
...
@@ -54,3 +54,11 @@ func Seed(seed int64) {
...
@@ -54,3 +54,11 @@ func Seed(seed int64) {
rng
.
rand
=
rand
.
New
(
rand
.
NewSource
(
seed
))
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
)
}
pkg/util/rand/rand_test.go
View file @
3758a01c
...
@@ -17,6 +17,7 @@ limitations under the License.
...
@@ -17,6 +17,7 @@ limitations under the License.
package
rand
package
rand
import
(
import
(
"math/rand"
"strings"
"strings"
"testing"
"testing"
)
)
...
@@ -35,3 +36,17 @@ func TestString(t *testing.T) {
...
@@ -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"
)
}
}
}
}
pkg/util/slice/slice.go
View file @
3758a01c
...
@@ -18,7 +18,7 @@ limitations under the License.
...
@@ -18,7 +18,7 @@ limitations under the License.
package
slice
package
slice
import
(
import
(
"math
/rand"
utilrand
"k8s.io/kubernetes/pkg/util
/rand"
"sort"
"sort"
)
)
...
@@ -41,7 +41,7 @@ func SortStrings(s []string) []string {
...
@@ -41,7 +41,7 @@ func SortStrings(s []string) []string {
// order. It returns a new slice.
// order. It returns a new slice.
func
ShuffleStrings
(
s
[]
string
)
[]
string
{
func
ShuffleStrings
(
s
[]
string
)
[]
string
{
shuffled
:=
make
([]
string
,
len
(
s
))
shuffled
:=
make
([]
string
,
len
(
s
))
perm
:=
rand
.
Perm
(
len
(
s
))
perm
:=
util
rand
.
Perm
(
len
(
s
))
for
i
,
j
:=
range
perm
{
for
i
,
j
:=
range
perm
{
shuffled
[
j
]
=
s
[
i
]
shuffled
[
j
]
=
s
[
i
]
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment