Skip to content

Commit 8554fa1

Browse files
committed
fix calling rand.New
1 parent eeaf5b0 commit 8554fa1

4 files changed

Lines changed: 13 additions & 23 deletions

File tree

seqkit/cmd/sample.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Attention:
9393
checkError(err)
9494
defer outfh.Close()
9595

96-
rand.Seed(seed)
96+
_rand := rand.New(rand.NewSource(seed))
9797
// randg := randomFloat64Generator(seed)
9898

9999
n := int64(0)
@@ -139,7 +139,7 @@ Attention:
139139
}
140140

141141
// if <-randg <= proportion {
142-
if rand.Float64() <= proportion {
142+
if _rand.Float64() <= proportion {
143143
n++
144144
record.FormatToWriter(outfh, config.LineWidth)
145145
if n == number {
@@ -163,7 +163,7 @@ Attention:
163163

164164
for _, record := range records {
165165
// if <-randg <= proportion {
166-
if rand.Float64() <= proportion {
166+
if _rand.Float64() <= proportion {
167167
n++
168168
record.FormatToWriter(outfh, config.LineWidth)
169169
if n == number {
@@ -194,7 +194,7 @@ Attention:
194194
}
195195

196196
// if <-randg <= proportion {
197-
if rand.Float64() <= proportion {
197+
if _rand.Float64() <= proportion {
198198
n++
199199
record.FormatToWriter(outfh, config.LineWidth)
200200
}

seqkit/cmd/shuffle.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/shenwei356/bio/seq"
3131
"github.com/shenwei356/bio/seqio/fai"
3232
"github.com/shenwei356/bio/seqio/fastx"
33-
"github.com/shenwei356/util/randutil"
3433
"github.com/shenwei356/xopen"
3534
"github.com/spf13/cobra"
3635
)
@@ -128,12 +127,14 @@ Attention:
128127
log.Infof("%d sequences loaded", len(sequences))
129128
log.Infof("shuffle ...")
130129
}
131-
rand.Seed(seed)
132130
indices := make([]int, len(index2name))
133131
for i := 0; i < len(index2name); i++ {
134132
indices[i] = i
135133
}
136-
randutil.Shuffle(indices)
134+
135+
rand.New(rand.NewSource(seed)).Shuffle(len(indices), func(i, j int) {
136+
indices[i], indices[j] = indices[j], indices[i]
137+
})
137138

138139
if !quiet {
139140
log.Infof("output ...")
@@ -219,12 +220,14 @@ Attention:
219220
if !quiet {
220221
log.Infof("shuffle ...")
221222
}
222-
rand.Seed(seed)
223223
indices := make([]int, len(index2name))
224224
for i := 0; i < len(index2name); i++ {
225225
indices[i] = i
226226
}
227-
randutil.Shuffle(indices)
227+
228+
rand.New(rand.NewSource(seed)).Shuffle(len(indices), func(i, j int) {
229+
indices[i], indices[j] = indices[j], indices[i]
230+
})
228231

229232
if !quiet {
230233
log.Infof("output ...")

seqkit/cmd/util.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"bytes"
2525
"fmt"
2626
"io"
27-
"math/rand"
2827
"os"
2928
"os/exec"
3029
"regexp"
@@ -492,18 +491,6 @@ func checkFileFormat(format string) {
492491
}
493492
}
494493

495-
// it's slow, do not use
496-
func randomFloat64Generator(seed int64) chan float64 {
497-
rand.Seed(seed)
498-
ch := make(chan float64, 1024)
499-
go func() {
500-
for {
501-
ch <- rand.Float64()
502-
}
503-
}()
504-
return ch
505-
}
506-
507494
func wrapByteSlice(s []byte, width int, buffer *bytes.Buffer) ([]byte, *bytes.Buffer) {
508495
if width < 1 {
509496
return s, buffer

tests/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ rm t.*
332332
file=tests/hairpin.fa
333333

334334
testseq() {
335-
cat $file | $app head -n 100 | $app rmdup
335+
cat $file | $app head -n 100 | $app rmdup --quiet
336336
}
337337
fun() {
338338
testseq | $app split -i -f

0 commit comments

Comments
 (0)