diff --git a/common.go b/common.go index ca763b9..456ed38 100644 --- a/common.go +++ b/common.go @@ -17,11 +17,12 @@ const ( // randStr returns a string of length n constructed // from pseudo-randomly selected characters from t. -func randStr(n int, t string) string { +// The pseudo-randomness uses random values from s. +func randStr(n int, t string, s rand.Source) string { l := len(t) - 1 chars := make([]byte, n) if l > 0 { - for i, p := range rand.New(rand.NewSource(1238)).Perm(n) { + for i, p := range rand.New(s).Perm(n) { chars[i] = t[p%l] } } diff --git a/common_test.go b/common_test.go index aec89f2..5a24403 100644 --- a/common_test.go +++ b/common_test.go @@ -4,29 +4,33 @@ package epi -import "testing" +import ( + "math/rand" + "testing" +) func TestRandStr(t *testing.T) { for _, test := range []struct { n int t string + s rand.Source }{ - {1, ""}, - {1, "a"}, - {1, "abc"}, - {10, "ab"}, - {10, "abcdefghijklmnopqrstuvwxyz"}, - {100, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}, + {1, "", rand.NewSource(1)}, + {1, "a", rand.NewSource(2)}, + {1, "abc", rand.NewSource(3)}, + {10, "ab", rand.NewSource(4)}, + {10, "abcdefghijklmnopqrstuvwxyz", rand.NewSource(5)}, + {100, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", rand.NewSource(6)}, } { - if got := len(randStr(test.n, test.t)); got != test.n { - t.Errorf("len(randStr(%d, %q) = %d; want %d", test.n, test.t, got, test.n) + if got := len(randStr(test.n, test.t, test.s)); got != test.n { + t.Errorf("len(randStr(%d, %q, %v) = %d; want %d", test.n, test.t, test.s, got, test.n) } } } func benchRandStr(b *testing.B, size int) { for i := 0; i < b.N; i++ { - randStr(size, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + randStr(size, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", rand.NewSource(int64(i))) } } diff --git a/dutchflag_test.go b/dutchflag_test.go index f338a85..279046e 100644 --- a/dutchflag_test.go +++ b/dutchflag_test.go @@ -36,7 +36,7 @@ func benchRearrange(b *testing.B, size int) { p := size / 3 q := size for i := 0; i < b.N; i++ { - data := rand.New(rand.NewSource(1234)).Perm(size) + data := rand.New(rand.NewSource(int64(i))).Perm(size) j := data[i%size]%(q-p) + p b.StartTimer() Rearrange(data, j) diff --git a/maxdiff_test.go b/maxdiff_test.go index 4871cc8..172d1f5 100644 --- a/maxdiff_test.go +++ b/maxdiff_test.go @@ -44,7 +44,7 @@ func TestMinBatteryCap(t *testing.T) { func benchMinBatteryCap(b *testing.B, size int) { b.StopTimer() for i := 0; i < b.N; i++ { - data := rand.New(rand.NewSource(1235)).Perm(size) + data := rand.New(rand.NewSource(int64(i))).Perm(size) b.StartTimer() MinBatteryCap(data) b.StopTimer() diff --git a/nextperm_test.go b/nextperm_test.go index 823b39a..c85b086 100644 --- a/nextperm_test.go +++ b/nextperm_test.go @@ -34,7 +34,7 @@ func TestNext(t *testing.T) { func benchNext(b *testing.B, size int) { b.StopTimer() for i := 0; i < b.N; i++ { - data := rand.New(rand.NewSource(1236)).Perm(size) + data := rand.New(rand.NewSource(int64(i))).Perm(size) b.StartTimer() NextPerm(data) b.StopTimer() diff --git a/powerset_test.go b/powerset_test.go index 6765438..6cd8e22 100644 --- a/powerset_test.go +++ b/powerset_test.go @@ -62,7 +62,7 @@ func benchPowerSet(b *testing.B, size int) { b.StopTimer() for i := 0; i < b.N; i++ { data := make([]interface{}, size) - for j, p := range rand.New(rand.NewSource(1237)).Perm(size) { + for j, p := range rand.New(rand.NewSource(int64(i))).Perm(size) { data[j] = p } b.StartTimer() diff --git a/reversewords_test.go b/reversewords_test.go index bd560a3..0c1a26b 100644 --- a/reversewords_test.go +++ b/reversewords_test.go @@ -4,7 +4,10 @@ package epi -import "testing" +import ( + "math/rand" + "testing" +) func TestReverseWords(t *testing.T) { for _, test := range []struct { @@ -31,7 +34,7 @@ func TestReverseWords(t *testing.T) { func benchReverseWords(b *testing.B, size int) { b.StopTimer() for i := 0; i < b.N; i++ { - s := randStr(size, "The quick brown fox jumps over the lazy dog") // Pangram. + s := randStr(size, "The quick brown fox jumps over the lazy dog", rand.NewSource(int64(i))) // Pangram. b.StartTimer() ReverseWords(s) b.StopTimer() diff --git a/rlecompr_test.go b/rlecompr_test.go index 5160631..ac95d9d 100644 --- a/rlecompr_test.go +++ b/rlecompr_test.go @@ -4,7 +4,10 @@ package epi -import "testing" +import ( + "math/rand" + "testing" +) func TestRLEEncode(t *testing.T) { for _, test := range []struct { @@ -40,7 +43,7 @@ func TestRLEEncode(t *testing.T) { func benchRLEEncode(b *testing.B, size int) { b.StopTimer() for i := 0; i < b.N; i++ { - s := randStr(size, "abcdefghijklmnopqrstuvwxyz") + s := randStr(size, "abcdefghijklmnopqrstuvwxyz", rand.NewSource(int64(i))) b.StartTimer() RLEEncode(s) b.StopTimer() @@ -87,7 +90,7 @@ func TestRLEDecode(t *testing.T) { func benchRLEDecode(b *testing.B, size int) { b.StopTimer() for i := 0; i < b.N; i++ { - s, ok := RLEEncode(randStr(size, "abcdefghijklmnopqrstuvwxyz")) + s, ok := RLEEncode(randStr(size, "abcdefghijklmnopqrstuvwxyz", rand.NewSource(int64(i)))) if !ok { b.Errorf("RLEEncode did not encode string properly") } diff --git a/spiralmetrix_test.go b/spiralmetrix_test.go index 64f8e26..23b1b92 100644 --- a/spiralmetrix_test.go +++ b/spiralmetrix_test.go @@ -51,7 +51,7 @@ func TestClockwise(t *testing.T) { func benchClockwise(b *testing.B, size int) { b.StopTimer() for i := 0; i < b.N; i++ { - ints := rand.New(rand.NewSource(1239)).Perm(size) + ints := rand.New(rand.NewSource(int64(i))).Perm(size) data := make([][]int, len(ints)) for j := range data { data[j] = append([]int(nil), ints...)