Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 创建rand结构_Go - Fatal编程技术网

Go 创建rand结构

Go 创建rand结构,go,Go,我在这个代码库中发现了以下内容,有人对这个方法发表了类似这样的评论 // TODO avoid using rand.Float64 method. it uses a singleton lock and may cause // performance issues. Instead, instantiate a rand struct and use that to call // Float64() func standardStrategy(l *le

我在这个代码库中发现了以下内容,有人对这个方法发表了类似这样的评论

    // TODO avoid using rand.Float64 method. it uses a singleton lock and may cause
    // performance issues. Instead, instantiate a rand struct and use that to call
    // Float64()

    func standardStrategy(l *ledger) bool {
        return rand.Float64() <= probabilitySend(l.Accounting.Value())
    }

func probabilitySend(ratio float64) float64 {
    x := 1 + math.Exp(6-3*ratio)
    y := 1 / x
    return 1 - y
}
//避免使用rand.Float64方法。它使用单例锁,可能导致
//性能问题。相反,实例化一个rand结构并使用它调用
//浮点数64()
func标准策略(l*分类账)bool{

return rand.Float64()我想它的意思是:
rand
包有一个名为
rand
struct的东西,它有随机生成函数,可能不会锁定全局锁,因此注释的作者可能是指使用这个结构。例如:

r := rand.New(rand.NewSource(1234))

fmt.Println(r.Float64())
此代码中使用的函数是包的全局函数,并使用全局初始化的
Rand
结构,称为internal
globalRand
,该结构具有内部互斥锁。因此避免使用它可以保存此锁定