我有从“localhost:8080/1”开始的go脚本,带有的上一个和的下一个链接,我需要添加具有可更改的自定义范围的随机链接,例如:
所以:
// Get next and previous page numbers
previous := new(big.Int).Sub(page, one)
next := new(big.Int).Add(page, one)
random :=????
最佳答案
您需要使用软件包 crypto.rand
Int()函数,该函数确实支持big.Int
(而不是 math.rand
package)
参见this article(及其playground example):
package main
import (
"fmt"
"math/big"
"crypto/rand"
)
func main() {
var prime1, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
// Generate random numbers in range [0..prime1]
// Ignore error values
// Don't use this code to generate secret keys that protect important stuff!
x, _ := rand.Int(rand.Reader, prime1)
y, _ := rand.Int(rand.Reader, prime1)
fmt.Printf("x: %v\n", x)
fmt.Printf("y: %v\n", y)
}
关于go - 在自定义范围内生成随机数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52675827/