github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/mapper/exprmapper/function/number/random/random.go (about)

     1  package random
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     8  
     9  	"github.com/TIBCOSoftware/flogo-lib/core/mapper/exprmapper/expression/function"
    10  	"github.com/TIBCOSoftware/flogo-lib/logger"
    11  )
    12  
    13  var log = logger.GetLogger("random-function")
    14  
    15  type Random struct {
    16  }
    17  
    18  func init() {
    19  	function.Registry(&Random{})
    20  }
    21  
    22  func (s *Random) GetName() string {
    23  	return "random"
    24  }
    25  
    26  func (s *Random) GetCategory() string {
    27  	return "number"
    28  }
    29  
    30  func (s *Random) Eval(limitIn interface{}) int {
    31  	limit, err := data.CoerceToInteger(limitIn)
    32  	if err != nil {
    33  		log.Errorf("Convert %+v to int error %s", limitIn, err.Error())
    34  		limit = 10
    35  	}
    36  	log.Debugf("Generate sudo-random integer number within the scope of [0, %d)", limit)
    37  	rand.Seed(time.Now().UnixNano())
    38  	return rand.Intn(limit)
    39  }