在 Go 中生成随机数
我试图在 Go 中生成随机数(整数),但无济于事。我在 crypto/rand
< 中找到了 rand
包/a>,这似乎是我想要的,但我无法从文档中得知如何使用它。这就是我现在正在尝试的:
b := []byte{}
something, err := rand.Read(b)
fmt.Printf("something = %v\n", something)
fmt.Printf("err = %v\n", err)
但不幸的是,这总是输出:
something = 0
err = <nil>
有没有办法解决这个问题,以便它实际上生成随机数?或者,有没有办法设置生成的随机数的上限?
I am trying to generate random numbers (integers) in Go, to no avail. I found the rand
package in crypto/rand
, which seems to be what I want, but I can't tell from the documentation how to use it. This is what I'm trying right now:
b := []byte{}
something, err := rand.Read(b)
fmt.Printf("something = %v\n", something)
fmt.Printf("err = %v\n", err)
But unfortunately this always outputs:
something = 0
err = <nil>
Is there a way to fix this so that it actually generates random numbers? Alternatively, is there a way to set the upper bound on the random numbers this generates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您的用例,另一个选项是 math/rand 包。如果您生成的数字需要完全不可预测,请不要这样做。不过,如果您需要获得可重现的结果,它会很有帮助 - 只需传递您第一次传递的相同种子即可。
这是经典的“用当前时间为生成器播种并生成一个数字”程序:
Depending on your use case, another option is the
math/rand
package. Don't do this if you're generating numbers that need to be completely unpredictable. It can be helpful if you need to get results that are reproducible, though -- just pass in the same seed you passed in the first time.Here's the classic "seed the generator with the current time and generate a number" program:
crypto/rand
仅提供随机数据的二进制流,但您可以使用encoding/binary
从中读取整数:crypto/rand
provides only binary stream of random data, but you can read integers from it usingencoding/binary
:自2012年4月1日起,lang的稳定版本发布后,您可以执行以下操作:
As of 1 april 2012, after the release of the stable version of the lang, you can do the following:
您还可以开发自己的随机数生成器,可能基于简单的“荒岛 PRNG”(线性同余生成器)。另外,请查找 L'Ecuyer (1999)、Mersenne Twister 或 Tausworthe 生成器...
https:// en.wikipedia.org/wiki/Pseudorandom_number_generator
(避免 RANDU,它在 1960 年代很流行,但是生成的随机数落在 3 空间中的 15 个超平面上)。
一些相关链接:
https://en.wikipedia.org/wiki/Lehmer_random_number_generator
使用此函数来更新您的 x[i+1],而不是上面的函数,
val = ((状态 * 1103515245) + 12345) & 0x7ffffffff 0x7fffffff
(基本上,a、c、m 的值不同)
https://www.redhat.com/en/blog/understanding-random-number-generators-and-their-limitations-linux
https://www.iro.umontreal.ca/~lecuyer/myftp/论文/handstat.pdf
https://www.math.utah.edu/~alfeld/Random/Random.html
https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/august/test-run-lightweight-random-number- Generation
You can also develop your own random number generator, perhaps based upon a simple "desert island PRNG", a Linear Congruential Generator. Also, look up L'Ecuyer (1999), Mersenne Twister, or Tausworthe generator...
https://en.wikipedia.org/wiki/Pseudorandom_number_generator
(Avoid RANDU, it was popular in the 1960's, but the random numbers generated fall on 15 hyperplanes in 3-space).
A few relevant links:
https://en.wikipedia.org/wiki/Lehmer_random_number_generator
You might use this function to update your x[i+1], instead of the one above,
val = ((state * 1103515245) + 12345) & 0x7fffffff
(basically, different values of a, c, m)
https://www.redhat.com/en/blog/understanding-random-number-generators-and-their-limitations-linux
https://www.iro.umontreal.ca/~lecuyer/myftp/papers/handstat.pdf
https://www.math.utah.edu/~alfeld/Random/Random.html
https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/august/test-run-lightweight-random-number-generation