- rand.Int()
- rand.Float64()
- rand.Intn(n) 生成0-n之间的随机数
package main
import (
"fmt"
"math/rand"
)
func main() {
{
value := rand.Int()
fmt.Println("value is ", value)
}
{
value := rand.Int()
fmt.Println("value is ", value)
}
/*
每次启动获取的值都是一样的
value is 5577006791947779410
value is 8674665223082153551
*/
}
通过随机数种子实现真正的随机数
{
// 通过随机种子,每次获取的随机数都是不一样的
rand.Seed(time.Now().UnixNano())
value := rand.Int()
fmt.Println("value is ", value)
}
{
value := rand.Int()
fmt.Println("value is ", value)
}
/*
每次启动获取的值都是一样的
value is 3374744019296468410
value is 7198434561365652743
*/