本文整理匯總了Golang中github.com/prataprc/monster/common.Scope.GetRandom方法的典型用法代碼示例。如果您正苦於以下問題:Golang Scope.GetRandom方法的具體用法?Golang Scope.GetRandom怎麽用?Golang Scope.GetRandom使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/prataprc/monster/common.Scope
的用法示例。
在下文中一共展示了Scope.GetRandom方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Bag
// Bag will fetch a random line from file and return it.
// args[0] - filename.
func Bag(scope common.Scope, args ...interface{}) interface{} {
var err error
filename := args[0].(string)
if !filepath.IsAbs(filename) {
if bagdir, _, ok := scope.GetString("_bagdir"); ok {
filename = filepath.Join(bagdir, filename)
} else if prodfile, _, ok := scope.GetString("_prodfile"); ok {
dirpath := filepath.Dir(prodfile)
filename = filepath.Join(dirpath, filename)
}
}
if filename, err = filepath.Abs(filename); err != nil {
panic(fmt.Errorf("bad filepath: %v\n", filename))
}
bagrw.RLock()
records, ok := cacheBagRecords[filename]
bagrw.RUnlock()
if !ok {
records = readBag(filename)
bagrw.Lock()
cacheBagRecords[filename] = records
bagrw.Unlock()
}
if len(records) > 0 {
rnd := scope.GetRandom()
record := records[rnd.Intn(len(records))]
if len(record) > 0 {
return record[0]
}
}
return ""
}
示例2: Rangef
// Rangef will randomly pick a value from args[0] to args[1]
// and return the same.
// args... are expected to be in float64
func Rangef(scope common.Scope, args ...interface{}) interface{} {
rnd := scope.GetRandom()
if len(args) == 2 {
min, max := args[0].(float64), args[1].(float64)
f := (rnd.Float64() * (max - min)) + min
return f
} else if len(args) == 1 {
max := args[0].(float64)
return rnd.Float64() * max
}
panic(fmt.Errorf("atleast one argument expected for range-form\n"))
}
示例3: Ranget
// Ranget will randomly pick a value from args[0] to args[1]
// and return the same.
// args... are expected to be in time.RFC3339 format
func Ranget(scope common.Scope, args ...interface{}) interface{} {
rnd := scope.GetRandom()
start, err := time.Parse(time.RFC3339, args[0].(string))
if err != nil {
panic(fmt.Errorf("parsing first argument %v: %v\n", args[0], err))
}
end, err := time.Parse(time.RFC3339, args[1].(string))
if err != nil {
panic(fmt.Errorf("parsing second argument %v: %v\n", args[0], err))
}
t := start.Add(time.Duration(rnd.Int63n(int64(end.Sub(start)))))
return t.Format(time.RFC3339)
}
示例4: Range
// Range will randomly pick a value from args[0] to args[1]
// and return the same.
// args... are expected to be in int64
func Range(scope common.Scope, args ...interface{}) interface{} {
var min, max int64
var err error
rnd := scope.GetRandom()
if len(args) == 2 {
min, max = args[0].(int64), args[1].(int64)
if err != nil {
panic(fmt.Errorf("parsing argument %v\n", args[1]))
}
} else if len(args) == 1 {
max = args[0].(int64)
} else {
panic(fmt.Errorf("atleast one argument expected for range-form\n"))
}
return rnd.Int63n(max-min) + min
}
示例5: Choice
// Choice will randomly pick one of the passed argument
// and return back.
func Choice(scope common.Scope, args ...interface{}) interface{} {
rnd := scope.GetRandom()
return args[rnd.Intn(len(args))]
}