/
builtin_random.go
46 lines (40 loc) · 1.27 KB
/
builtin_random.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package expreduce
import (
"math/big"
"math/rand"
"github.com/corywalker/expreduce/expreduce/atoms"
"github.com/corywalker/expreduce/pkg/expreduceapi"
)
func getRandomDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "RandomReal",
Details: "`SeedRandom[UnixTime[]]` is called automatically upon " +
"initialization of Expreduce, so random number sequences will not " +
"repeat over subsequent sessions.",
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 1 {
return this
}
return atoms.NewReal(big.NewFloat(rand.Float64()))
},
})
defs = append(defs, Definition{
Name: "SeedRandom",
Details: "`SeedRandom[UnixTime[]]` is called automatically upon " +
"initialization of Expreduce, so random number sequences will not " +
"repeat over subsequent sessions.",
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 2 {
return this
}
asInt, isInt := this.GetParts()[1].(*atoms.Integer)
if isInt {
//nolint:staticcheck
rand.Seed(asInt.Val.Int64())
return atoms.NewSymbol("System`Null")
}
return this
},
})
return
}