使用Go的Rand包时遇到的困难

huangapple go评论65阅读模式
英文:

Difficulty with Go Rand package

问题

有没有任何Go函数在每次运行时都返回真伪随机数?我的意思是,考虑以下代码,

package main

import (
    "fmt"
    "rand"
)

func main() {
    fmt.Println(rand.Int31n(100))
}

每次我执行这段代码时,都会得到相同的输出。有没有一种方法,每次调用时都返回不同的随机结果?

英文:

Is there any Go function which returns true pseudo random number in every run? What I actually mean is, consider following code,

package main

import (
    "fmt"
    "rand"
)

func main() {
    fmt.Println(rand.Int31n(100))
}

Every time I execute this code, I get the same output. Is there a method that will return different, random results each time that it is called?

答案1

得分: 27

rand可以用来生成伪随机数,这些数是基于特定初始值(称为“种子”)生成的。

一个常见的选择是使用当前时间的纳秒数作为初始种子,这个值在多次执行程序时可能会有所不同。你可以使用以下代码将随机生成器初始化为当前时间:

rand.Seed(time.Now().UnixNano())

(别忘了导入time包)

还有另一个包叫做crypto/rand,可以用来生成更好的随机值(这个生成器可能还会考虑用户的鼠标移动、处理器的当前热度和许多其他因素)。然而,这个包中的函数速度要慢几倍,并且除非你要编写一个密码生成器(或其他与安全相关的东西),否则普通的rand包可能就足够了。

英文:

The package rand can be used to generate pseudo random numbers, which are generated based on a specific initial value (called "seed").

A popular choice for this initial seed is for example the current time in nanoseconds - a value which will probably differ when you execute your program multiple times. You can initialize the random generator with the current time with something like this:

rand.Seed(time.Now().UnixNano())

(don't forget to import the time package for that)

There is also another package called crypto/rand which can be used to generate better random values (this generator might also take the user's mouse movements, the current heat of the processor and a lot of other factors into account). However, the functions in this package are several times slower and, unless you don't write a pass-phrase generator (or other security related stuff), the normal rand package is probably fine.

答案2

得分: 2

你必须先种子随机数生成器。

我从来没有用过Go,但可能是rand.Seed(x);

英文:

You have to seed the RNG first.

I've never used Go, but it's probably rand.Seed(x);

答案3

得分: 0

rand.Seed(time.Now().UnixNano()) 在 Ubuntu 上有效。我花了很长时间研究 rand.Seed(time.Nanoseconds())。最后我在 golang tour 上找到了示例:Google Search 2.1。

英文:

rand.Seed(time.Now().UnixNano()) works on Ubuntu. I spent forever researching rand.Seed(time.Nanoseconds()). I finally found the Example: Google Search 2.1 on the golang tour.

答案4

得分: 0

总结以上的答案,你可以使用rand.Seed(n)来编写自己的随机数生成器。n必须是每次种子时不同的数字。以下是一个示例:

func RandomInt(n int) int {
    rand.Seed(time.Now().UnixNano())
    return rand.Intn(n)
}

func RandomIntFromRange(min int, max int) int {
    rand.Seed(time.Now().UnixNano())
    return rand.Intn(max - min + 1) + min
}
英文:

To sum up the answers above, you can write your own random number generator with rand.Seed(n). n must be a different number each time you seed. Here is an example:

func RandomInt(n int) int {
    rand.Seed(time.Now().UnixNano())
    return rand.Intn(n)
}

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

huangapple
  • 本文由 发表于 2011年11月28日 04:13:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/8288679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定