Ai
1 Star 0 Fork 0

叶明志/golang练习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rand.go 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
package rand1
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"math/big"
)
func rand1Num() {
for n := 128; n < 140; n++ {
b := new(big.Int).SetInt64(int64(n)) //将new(big.Int)设为int64(n)并返回new(big.Int)
fmt.Printf("max Int is : %v ", b)
i, err := rand.Int(rand.Reader, b)
if err != nil {
fmt.Printf("Can't generate random value: %v, %v", i, err)
}
fmt.Printf("rand Int is : %v\n", i)
}
}
//sessionID ID,即session的唯一标识符
func sessionID() string {
b := make([]byte, 32)
//ReadFull从rand.Reader精确地读取len(b)字节数据填充进b
//rand.Reader是一个全局、共享的密码用强随机数生成器
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return ""
}
fmt.Println(b) //[62 186 123 16 209 19 130 218 146 136 171 211 12 233 45 99 80 200 59 20 56 254 170 110 59 147 223 177 48 136 220 142]
return base64.URLEncoding.EncodeToString(b) //将生成的随机数b编码后返回字符串,该值则作为session ID
}
func prime() {
n := 100
p, _ := rand.Prime(rand.Reader, n) //n代表位数,比如3为2位,127为7位
fmt.Println(p.Bytes())
b := p.Bytes()
str := base64.URLEncoding.EncodeToString(b)
fmt.Println(str)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yemingzhi/GolangLearnPractice1.git
git@gitee.com:yemingzhi/GolangLearnPractice1.git
yemingzhi
GolangLearnPractice1
golang练习
2bf136849dce

搜索帮助