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
This is more POSIX-compatible. Fixes #231
  • Loading branch information
benhoyt committed Sep 14, 2024
1 parent f39db9b commit 91f42ce
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 @@ -616,6 +616,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 91f42ce

Please sign in to comment.