我的代码:
```
package main
import (
"fmt"
"math/rand"
//"runtime"
"time"
)
func main() {
//runtime.GOMAXPROCS(1)
fmt.Println("===rand.New(rand.NewSource(64))====")
for i := 0; i < 20; i++ {
r := rand.New(rand.NewSource(64))
fmt.Print(fmt.Sprint(r.Intn(5)) + "-")
}
fmt.Println("")
fmt.Println("===rand.New(rand.NewSource(time.Now().UnixNano()))====")
for i := 0; i < 20; i++ {
r2 := rand.New(rand.NewSource(time.Now().UnixNano()))
fmt.Print(fmt.Sprint(r2.Intn(5)) + "-")
}
fmt.Println("")
fmt.Println("====rand.Seed(time.Now().UnixNano())===")
for i := 0; i < 20; i++ {
rand.Seed(time.Now().UnixNano())
fmt.Print(fmt.Sprint(rand.Intn(5)) + "-")
}
}
```
输出的效果为:
G:\goWorkspace\src>go run main.go
===rand.New(rand.NewSource(64))====
3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-
===rand.New(rand.NewSource(time.Now().UnixNano()))====
1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-
====rand.Seed(time.Now().UnixNano())===
1-4-4-4-4-4-4-4-4-4-4-4-4-4-4-3-3-3-3-3-
G:\goWorkspace\src>go run main.go
===rand.New(rand.NewSource(64))====
3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-
===rand.New(rand.NewSource(time.Now().UnixNano()))====
1-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-4-4-
====rand.Seed(time.Now().UnixNano())===
4-4-4-4-4-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-
G:\goWorkspace\src>go run main.go
===rand.New(rand.NewSource(64))====
3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-
===rand.New(rand.NewSource(time.Now().UnixNano()))====
4-4-4-4-4-4-3-3-3-3-3-3-3-3-3-3-3-3-3-3-
====rand.Seed(time.Now().UnixNano())===
1-1-1-1-1-1-1-2-2-2-2-2-2-2-2-2-2-2-2-2-
>**环境:**
>go版本 1.4
>操作系统:win8 64bit
>有什么方法:让随机数更好的生成吗?
更多评论
你自己的代码写的有问题,比如最后一个,改成这样试试:
```golang
rand.Seed(time.Now().UnixNano())
for i := 0; i < 20; i++ {
fmt.Print(fmt.Sprint(rand.Intn(5)) + "-")
}
```
#1