123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package stdlib
- import (
- "math/rand"
- "github.com/d5/tengo/v2"
- )
- var randModule = map[string]tengo.Object{
- "int": &tengo.UserFunction{
- Name: "int",
- Value: FuncARI64(rand.Int63),
- },
- "float": &tengo.UserFunction{
- Name: "float",
- Value: FuncARF(rand.Float64),
- },
- "intn": &tengo.UserFunction{
- Name: "intn",
- Value: FuncAI64RI64(rand.Int63n),
- },
- "exp_float": &tengo.UserFunction{
- Name: "exp_float",
- Value: FuncARF(rand.ExpFloat64),
- },
- "norm_float": &tengo.UserFunction{
- Name: "norm_float",
- Value: FuncARF(rand.NormFloat64),
- },
- "perm": &tengo.UserFunction{
- Name: "perm",
- Value: FuncAIRIs(rand.Perm),
- },
- "seed": &tengo.UserFunction{
- Name: "seed",
- Value: FuncAI64R(rand.Seed),
- },
- "read": &tengo.UserFunction{
- Name: "read",
- Value: func(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- return nil, tengo.ErrWrongNumArguments
- }
- y1, ok := args[0].(*tengo.Bytes)
- if !ok {
- return nil, tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "bytes",
- Found: args[0].TypeName(),
- }
- }
- res, err := rand.Read(y1.Value)
- if err != nil {
- ret = wrapError(err)
- return
- }
- return tengo.Int{Value: int64(res)}, nil
- },
- },
- "rand": &tengo.UserFunction{
- Name: "rand",
- Value: func(args ...tengo.Object) (tengo.Object, error) {
- if len(args) != 1 {
- return nil, tengo.ErrWrongNumArguments
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- return nil, tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- }
- src := rand.NewSource(i1)
- return randRand(rand.New(src)), nil
- },
- },
- }
- func randRand(r *rand.Rand) *tengo.ImmutableMap {
- return &tengo.ImmutableMap{
- Value: map[string]tengo.Object{
- "int": &tengo.UserFunction{
- Name: "int",
- Value: FuncARI64(r.Int63),
- },
- "float": &tengo.UserFunction{
- Name: "float",
- Value: FuncARF(r.Float64),
- },
- "intn": &tengo.UserFunction{
- Name: "intn",
- Value: FuncAI64RI64(r.Int63n),
- },
- "exp_float": &tengo.UserFunction{
- Name: "exp_float",
- Value: FuncARF(r.ExpFloat64),
- },
- "norm_float": &tengo.UserFunction{
- Name: "norm_float",
- Value: FuncARF(r.NormFloat64),
- },
- "perm": &tengo.UserFunction{
- Name: "perm",
- Value: FuncAIRIs(r.Perm),
- },
- "seed": &tengo.UserFunction{
- Name: "seed",
- Value: FuncAI64R(r.Seed),
- },
- "read": &tengo.UserFunction{
- Name: "read",
- Value: func(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- return nil, tengo.ErrWrongNumArguments
- }
- y1, ok := args[0].(*tengo.Bytes)
- if !ok {
- return nil, tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "bytes",
- Found: args[0].TypeName(),
- }
- }
- res, err := r.Read(y1.Value)
- if err != nil {
- ret = wrapError(err)
- return
- }
- return tengo.Int{Value: int64(res)}, nil
- },
- },
- },
- }
- }
|