Skip to content

Commit 012adf2

Browse files
authored
Make srand() use unix time in seconds, and set seed for next srand() (#241)
This is more POSIX-compatible. Fixes #231
1 parent 6e1b1b4 commit 012adf2

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

interp/interp_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ BEGIN {
621621
print (a==x, b==y, c==z)
622622
}
623623
`, "", "0 0 0 0\n1 1 1\n", "", ""},
624+
{`BEGIN { s = srand(srand()); print (s > 1700000000) }`, "", "1\n", "", ""},
624625
{`
625626
BEGIN {
626627
for (i = 0; i < 1000; i++) {

interp/vm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,8 @@ func (p *interp) callBuiltin(builtinOp compiler.BuiltinOp) error {
10211021

10221022
case compiler.BuiltinSrand:
10231023
prevSeed := p.randSeed
1024-
p.random.Seed(time.Now().UnixNano())
1024+
p.randSeed = float64(time.Now().Unix())
1025+
p.random.Seed(int64(math.Float64bits(p.randSeed)))
10251026
p.push(num(prevSeed))
10261027

10271028
case compiler.BuiltinSrandSeed:

0 commit comments

Comments
 (0)