为什么这个会重复相同的随机数?

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

Why this repeats the same random number?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Go语言还不熟悉,不确定为什么每次运行时rand.Intn(n int) int都会打印相同的数字:

package main

import (
	"fmt"
	"math/rand"
)
 

func main() {
	fmt.Println(rand.Intn(10)) 
}

文档中说:

Intn返回一个非负的伪随机数,范围在[0,n)之间,使用默认的Source。如果n <= 0,则会引发panic。

那么如何正确地初始化随机数生成器的种子呢?

英文:

I'm new to Go and not sure why it prints the same number for rand.Intn(n int) int for every run:

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
)
	 

func main() {
	fmt.Println(rand.Intn(10)) 
}

The docs says :

> Intn returns, as an int, a non-negative pseudo-random number in [0,n) from the default Source. It panics if n <= 0.

And how do I properly seed the random number generation?

答案1

得分: 13

通过调用rand.Seed()函数,并传递一个(随机的)种子(通常是当前的Unix时间戳),可以初始化默认的随机数生成器。引用自math/rand包的文档:

顶层函数(如Float64和Int)使用一个默认的共享Source,在每次运行程序时产生确定性的值序列。如果需要每次运行时都有不同的行为,则使用Seed函数来初始化默认的Source。

示例:

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

如果没有调用rand.Seed(),生成器的行为就像被种子1初始化一样:

Seed函数使用提供的种子值将默认的Source初始化为确定性状态。如果没有调用Seed函数,生成器的行为就像被Seed(1)种子初始化一样。

英文:

By calling the rand.Seed() function, passing it a (random) seed (typically the current unix timestamp). Quoting from math/rand package doc:

> Top-level functions, such as Float64 and Int, use a default shared Source that produces a deterministic sequence of values each time a program is run. Use the Seed function to initialize the default Source if different behavior is required for each run.

Example:

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

If rand.Seed() is not called, the generator behaves as if seeded by 1:

> Seed uses the provided seed value to initialize the default Source to a deterministic state. If Seed is not called, the generator behaves as if seeded by Seed(1).

答案2

得分: 2

package main

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

func randomGen(min, max int) int {
	rand.Seed(time.Now().Unix())
	return rand.Intn(max - min) + min
}

func main() {
	randNum := randomGen(1, 10)
	fmt.Println(randNum)
}
package main

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

func randomGen(min, max int) int {
	rand.Seed(time.Now().Unix())
	return rand.Intn(max - min) + min
}

func main() {
	randNum := randomGen(1, 10)
	fmt.Println(randNum)
}
package main

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

func randomGen(min, max int) int {
	rand.Seed(time.Now().Unix())
	return rand.Intn(max - min) + min
}

func main() {
	randNum := randomGen(1, 10)
	fmt.Println(randNum)
}
英文:
package main

import

(
&quot;fmt&quot;
&quot;math/rand&quot;
&quot;time&quot;
)

func randomGen(min, max int) int {
rand.Seed(time.Now().Unix())
return rand.Intn(max - min) + min
}

func main() {
randNum := randomGen(1, 10)
fmt.Println(randNum)
}

答案3

得分: -1

在包* math/rand *下,您可以找到一个类型Rand

> func New(src Source) *Rand - New返回一个新的Rand,它使用src中的随机值生成其他随机值。

实际上,它需要一个种子来生成。

  1. 第一步:使用new Source创建一个种子作为源。
    time.Now().UnixNano()用于提高准确性。
  2. 第二步:从种子创建一个类型Rand
  3. 第三步:生成一个随机数。

示例:

package main

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

func main() {  
    source := rand.NewSource(time.Now().UnixNano())    
    r := rand.New(source)    
    fmt.Println(r.Intn(100))    
}
英文:

Under the package, math/rand, you can find a type Rand.

> func New(src Source) *Rand - New returns a new Rand that uses random
> values from src to generate other random values.

It actually needs a seed to generate it.

  1. step 1: create a seed as a source using new Source.
    time.Now().UnixNano() is used for the accuracy.
  2. step 2: create a
    type Rand from the seed
  3. step 3: generate a random number.

Example:

package main

import (
    &quot;fmt&quot;
    &quot;math/rand&quot;
    &quot;time&quot;
)   

func main() {  
    source := rand.NewSource(time.Now().UnixNano())    
    r := rand.New(source)    
    fmt.Println(r.Intn(100))    
}

huangapple
  • 本文由 发表于 2017年8月18日 17:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/45753397.html
匿名

发表评论

匿名网友

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

确定