相对于 math/rand 随机数,crypto/rand 随机数更加复杂并且不可预测
1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import (
"crypto/rand"
"encoding/binary"
"fmt"
)
func main() {
var n int32
binary.Read(rand.Reader, binary.LittleEndian, &n)
fmt.Println(n)
}
下面是使用math/rand 生成随机数的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
fmt.Println(rand.Intn(100))
}
本文网址: https://golangnote.com/topic/201.html 转摘请注明来源