Skip to content

Commit

Permalink
fix: rand.Seed() deparcated (#624)
Browse files Browse the repository at this point in the history
* fix rand.Seed

fix rand.Seed

* fix rand.Seed

fix rand.Seed

fix unexpected format change

fix unexpected format change

* fix unexpected format change

---------

Co-authored-by: Andrii Siriak <[email protected]>
  • Loading branch information
hideyuda and siriak authored Feb 14, 2023
1 parent 97d4a14 commit 27c627e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cipher/caesar/caesar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ func Example() {
}

func FuzzCaesar(f *testing.F) {
rand.Seed(time.Now().Unix())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
f.Add("The Quick Brown Fox Jumps over the Lazy Dog.")
f.Fuzz(func(t *testing.T, input string) {
key := rand.Intn(26)
key := rnd.Intn(26)
if result := Decrypt(Encrypt(input, key), key); result != input {
t.Fatalf("With input: '%s' and key: %d\n\tExpected: '%s'\n\tGot: '%s'", input, key, input, result)
}
Expand Down
14 changes: 7 additions & 7 deletions graph/lowestcommonancestor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ func TestLCA(t *testing.T) {
}

func generateTree() *Tree {
rand.Seed(time.Now().UnixNano())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

const MAXVERTEX int = 2000
var numbersVertex int = rand.Intn(MAXVERTEX) + 1
var root int = rand.Intn(numbersVertex)
var numbersVertex int = rnd.Intn(MAXVERTEX) + 1
var root int = rnd.Intn(numbersVertex)
var edges []TreeEdge

var fullGraph []TreeEdge
Expand All @@ -163,7 +163,7 @@ func generateTree() *Tree {
})
}
}
rand.Shuffle(len(fullGraph), func(i, j int) {
rnd.Shuffle(len(fullGraph), func(i, j int) {
fullGraph[i], fullGraph[j] = fullGraph[j], fullGraph[i]
})

Expand Down Expand Up @@ -201,7 +201,7 @@ func generateTree() *Tree {
}

func generateQuery(tree *Tree) []Query {
rand.Seed(time.Now().UnixNano())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
const MAXQUERY = 50
var queries []Query

Expand All @@ -217,8 +217,8 @@ func generateQuery(tree *Tree) []Query {
}

for q := 1; q <= MAXQUERY; q++ {
u := rand.Intn(tree.numbersVertex)
v := rand.Intn(tree.numbersVertex)
u := rnd.Intn(tree.numbersVertex)
v := rnd.Intn(tree.numbersVertex)
queries = append(queries, Query{
u: u,
v: v,
Expand Down
4 changes: 2 additions & 2 deletions sort/sorts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ func TestMergeParallel(t *testing.T) {

// Test parallel merge sort with a large slice
t.Run("ParallelMerge on large slice", func(t *testing.T) {
rand.Seed(time.Now().UnixNano())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
size := 100000
randomLargeSlice := make([]int, size)
for i := range randomLargeSlice {
randomLargeSlice[i] = rand.Intn(size)
randomLargeSlice[i] = rnd.Intn(size)
}
sortedSlice := sort.ParallelMerge[int](randomLargeSlice)
for i := 0; i < len(sortedSlice)-1; i++ {
Expand Down
16 changes: 8 additions & 8 deletions strings/genetic/genetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
debug := conf.Debug

// Just a seed to improve randomness required by the algorithm
rand.Seed(time.Now().UnixNano())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

// Verify that the target contains no genes besides the ones inside genes variable.
for position, r := range target {
Expand All @@ -113,7 +113,7 @@ func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
for i := 0; i < populationNum; i++ {
key := ""
for x := 0; x < utf8.RuneCountInString(target); x++ {
choice := rand.Intn(len(charmap))
choice := rnd.Intn(len(charmap))
key += string(charmap[choice])
}
pop[i] = PopulationItem{key, 0}
Expand Down Expand Up @@ -165,18 +165,18 @@ func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
nChild = 10
}
for x := 0.0; x < nChild; x++ {
parent2 := pop[rand.Intn(selectionNum)]
parent2 := pop[rnd.Intn(selectionNum)]
// Crossover
split := rand.Intn(utf8.RuneCountInString(target))
split := rnd.Intn(utf8.RuneCountInString(target))
child1 := append([]rune(parent1.Key)[:split], []rune(parent2.Key)[split:]...)
child2 := append([]rune(parent2.Key)[:split], []rune(parent1.Key)[split:]...)
// Clean fitness value
// Mutate
if rand.Float64() < mutationProb {
child1[rand.Intn(len(child1))] = charmap[rand.Intn(len(charmap))]
if rnd.Float64() < mutationProb {
child1[rnd.Intn(len(child1))] = charmap[rnd.Intn(len(charmap))]
}
if rand.Float64() < mutationProb {
child2[rand.Intn(len(child2))] = charmap[rand.Intn(len(charmap))]
if rnd.Float64() < mutationProb {
child2[rnd.Intn(len(child2))] = charmap[rnd.Intn(len(charmap))]
}
// Push into 'popChildren'
popChildren = append(popChildren, PopulationItem{string(child1), 0})
Expand Down
4 changes: 2 additions & 2 deletions structure/tree/avl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ func TestAVLDelete(t *testing.T) {
t.Run("Random Test", func(t *testing.T) {
nums := []int{100, 500, 1000, 10_000}
for _, n := range nums {
rand.Seed(time.Now().Unix())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
tree := bt.NewAVL[int]()
nums := rand.Perm(n)
nums := rnd.Perm(n)
tree.Push(nums...)

rets := tree.InOrder()
Expand Down
4 changes: 2 additions & 2 deletions structure/tree/bstree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func TestDelete(t *testing.T) {
t.Run("Random Test", func(t *testing.T) {
tests := []int{100, 500, 1000, 10_000}
for _, n := range tests {
rand.Seed(time.Now().Unix())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
tree := bt.NewBinarySearch[int]()
nums := rand.Perm(n)
nums := rnd.Perm(n)
tree.Push(nums...)

rets := tree.InOrder()
Expand Down
4 changes: 2 additions & 2 deletions structure/tree/rbtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func TestRBTreeDelete(t *testing.T) {
func TestRBTree(t *testing.T) {
testcases := []int{100, 200, 1000, 10000}
for _, n := range testcases {
rand.Seed(time.Now().Unix())
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
tree := bt.NewRB[int]()
nums := rand.Perm(n)
nums := rnd.Perm(n)
tree.Push(nums...)

rets := tree.InOrder()
Expand Down

0 comments on commit 27c627e

Please sign in to comment.