Skip to content

Commit

Permalink
Make srand() use unix time in seconds, and set seed for next srand() (#…
Browse files Browse the repository at this point in the history
…241)

This is more POSIX-compatible. Fixes #231
  • Loading branch information
benhoyt authored Sep 14, 2024
1 parent 6e1b1b4 commit 012adf2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ BEGIN {
print (a==x, b==y, c==z)
}
`, "", "0 0 0 0\n1 1 1\n", "", ""},
{`BEGIN { s = srand(srand()); print (s > 1700000000) }`, "", "1\n", "", ""},
{`
BEGIN {
for (i = 0; i < 1000; i++) {
Expand Down
3 changes: 2 additions & 1 deletion interp/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ func (p *interp) callBuiltin(builtinOp compiler.BuiltinOp) error {

case compiler.BuiltinSrand:
prevSeed := p.randSeed
p.random.Seed(time.Now().UnixNano())
p.randSeed = float64(time.Now().Unix())
p.random.Seed(int64(math.Float64bits(p.randSeed)))
p.push(num(prevSeed))

case compiler.BuiltinSrandSeed:
Expand Down

0 comments on commit 012adf2

Please sign in to comment.