Go 语言

Go 语言教程 Go 语言环境安装 Go 语言结构 Go 语言基础语法 Go 语言数据类型 Go 语言变量 Go 语言常量 Go 语言运算符 Go 语言条件语句 Go 语言 if 语句 Go 语言 if...else 语句 Go 语言 if 语句嵌套 Go 语言 switch 语句 Go 语言 select 语句 Go 语言循环语句 Go 语言 for 循环 Go 语言循环嵌套 Go 语言 break 语句 Go 语言 continue 语句 Go 语言 goto 语句 Go 语言函数 Go 语言函数值传递值 Go 语言函数引用传递值 Go 语言函数作为值 Go 语言函数闭包 Go 语言函数方法 Go 语言变量作用域 Go 语言数组 Go 语言多维数组 Go 语言向函数传递数组 Go 语言指针 Go 语言指针数组 Go 语言指向指针的指针 Go 语言指针作为函数参数 Go 语言结构体 Go 语言切片(Slice) Go 语言范围(Range) Go 语言Map(集合) Go 语言递归函数 Go 语言类型转换 Go 语言接口 Go 错误处理 Go 语言开发工具Go 语言标准库

Go 语言标准库


package rand

import "crypto/rand"

rand包实现了用于加解密的更安全的随机数生成器。

Go语言标准库 >>


  • Variables
  • func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)
  • func Prime(rand io.Reader, bits int) (p *big.Int, err error)
  • func Read(b []byte) (n int, err error)
  • Examples

    Go语言标准库 >>


  • Read
  • Variables

    var Reader io.Reader

    Reader是一个全局、共享的密码用强随机数生成器。在Unix类型系统中,会从/dev/urandom读取;而Windows中会调用CryptGenRandom API。

    func Int

    func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)

    返回一个在[0, max)区间服从均匀分布的随机值,如果max<=0则会panic。

    func Prime

    func Prime(rand io.Reader, bits int) (p *big.Int, err error)

    返回一个具有指定字位数的数字,该数字具有很高可能性是质数。如果从rand读取时出错,或者bits<2会返回错误。

    func Read

    func Read(b []byte) (n int, err error)

    本函数是一个使用io.ReadFull调用Reader.Read的辅助性函数。当且仅当err == nil时,返回值n == len(b)。

    Example
    c := 10
    b := make([]byte, c)
    _, err := rand.Read(b)
    if err != nil {
        fmt.Println("error:", err)
        return
    }
    // The slice should now contain random bytes instead of only zeroes.
    fmt.Println(bytes.Equal(b, make([]byte, c)))

    Output:

    false