Golang随机数生成器如何正确播种 [英] Golang random number generator how to seed properly

查看:111
本文介绍了Golang随机数生成器如何正确播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  package main 

进口(
字节
fmt
数学/兰德
时间


func main(){
fmt.Println(randomString(10))
}

func randomString(l int)string {
var结果字节。缓冲区
var temp字符串
for i:= 0;我<升; {
if string(randInt(65,90))!= temp {
temp = string(randInt(65,90))
result.WriteString(temp)
i ++


return result.String()
}

func randInt(min int,max int)int {
rand.Seed (time.Now()。UTC()。UnixNano())
return min + rand.Intn(max-min)
}

我的实现非常慢。使用 time >进行播种会在一定时间内带来相同的随机数,因此循环会一次又一次地迭代。我怎样才能改进我的代码?

解决方案

每次设置相同的种子时,都会得到相同的序列。所以当然,如果你在快速循环中设置种子的时间,你可能会多次使用相同的种子。



在你的情况下,因为你打电话给你的 randInt 函数,直到你有一个不同的值,你就等着时间(由Nano返回)改变。



至于所有伪随机库,您必须设置只有一次,例如当初始化你的程序,除非你特别需要重现一个给定的序列(这通常是为了调试)。



之后,你只需调用 Intn 来获得下一个随机整数。



移动 rand.Seed(time.Now ().UTC()。UnixNano())从randInt函数到主函数的开始,一切都会更快。



请注意,我认为您可以简化您的字符串构建:

 包主要

进口(
fmt
数学/兰特
时间


func main(){
rand.Seed(time.Now()。UTC()。UnixNano())
fmt.Println(randomString(10))
}

func randomString(l int)string {
bytes:= make([] byte,l)
for i:= 0;我<升; i ++ {
bytes [i] = byte(randInt(65,90))
}
返回字符串(字节)
}

func randInt min int,max int)int {
return min + rand.Intn(max-min)
}


I am trying to generate a random string in Go and here is the code I have written so far:

package main

import (
    "bytes"
    "fmt"
    "math/rand"
    "time"
)

func main() {
    fmt.Println(randomString(10))
}

func randomString(l int) string {
    var result bytes.Buffer
    var temp string
    for i := 0; i < l; {
        if string(randInt(65, 90)) != temp {
            temp = string(randInt(65, 90))
            result.WriteString(temp)
            i++
        }
    }
    return result.String()
}

func randInt(min int, max int) int {
    rand.Seed(time.Now().UTC().UnixNano())
    return min + rand.Intn(max-min)
}

My implementation is very slow. Seeding using time brings the same random number for a certain time, so the loop iterates again and again. How can I improve my code?

解决方案

Each time you set the same seed, you get the same sequence. So of course if you're setting the seed to the time in a fast loop, you'll probably call it with the same seed many times.

In your case, as you're calling your randInt function until you have a different value, you're waiting for the time (as returned by Nano) to change.

As for all pseudo-random libraries, you have to set the seed only once, for example when initializing your program unless you specifically need to reproduce a given sequence (which is usually done for debugging).

After that you simply call Intn to get the next random integer.

Move the rand.Seed( time.Now().UTC().UnixNano()) line from the randInt function to the start of the main and everything will be faster.

Note also that I think you can simplify your string building:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UTC().UnixNano())
    fmt.Println(randomString(10))
}

func randomString(l int) string {
    bytes := make([]byte, l)
    for i := 0; i < l; i++ {
        bytes[i] = byte(randInt(65, 90))
    }
    return string(bytes)
}

func randInt(min int, max int) int {
    return min + rand.Intn(max-min)
}

这篇关于Golang随机数生成器如何正确播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆