Skip to content

Commit

Permalink
Merge pull request #2 from t-mrt/once_seed
Browse files Browse the repository at this point in the history
fix generated strings is not random
  • Loading branch information
t-mrt authored Feb 19, 2017
2 parents bd8de43 + 0e6ab92 commit d605a73
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions gocha.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Gocha interface {

func New(pattern string) (error, Gocha) {

rand.Seed(time.Now().UnixNano())

exp, err := syntax.Parse(pattern, syntax.Perl)
if err != nil {
return err, nil
Expand Down Expand Up @@ -53,10 +55,7 @@ func (g gocha) Gen() string {

case syntax.InstAlt:

rand.Seed(time.Now().UnixNano())
i := rand.Intn(10)

if i%2 == 1 {
if i := rand.Intn(2); i%2 == 1 {
pc = prog.Inst[pc].Out
} else {
pc = prog.Inst[pc].Arg
Expand Down Expand Up @@ -143,13 +142,11 @@ func randFromRange(rs []intRange) int {
for _, r := range rs {
overallLen = overallLen + (r.b - r.a + 1)
}
rand.Seed(time.Now().UnixNano())
index := rand.Intn(overallLen)
var result int
for _, r := range rs {

if (r.b - r.a) >= index {
rand.Seed(time.Now().UnixNano())
result = rand.Intn(r.b-r.a+1) + r.a
break
}
Expand Down

0 comments on commit d605a73

Please sign in to comment.