📜  Golang 中的 rand 包

📅  最后修改于: 2021-10-24 14:17:51             🧑  作者: Mango

Go 语言在 math/rand 包的帮助下为生成指定类型的随机数提供了内置支持。这个包实现了伪随机数生成器。这些随机数是由一个源生成的,每次程序运行时,这个源都会产生一个确定性的值序列。如果你想为安全敏感的工作随机数,那么使用 crypto/rand 包。

注意:在这个包中,使用了数学区间符号,例如 [0, n)。

Function Description
ExpFloat64 This function returns an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution from the default source.
Float32 This function returns, as a float32, a pseudo-random number in [0.0, 1.0) from the default source.
Float64 This function returns, as a float64, a pseudo-random number in [0.0, 1.0) from the default source.
Int This function returns a non-negative pseudo-random int from the default source.
Int31 This function returns a non-negative pseudo-random 31-bit integer as an int32 from the default source.
Int31n This function returns, as an int32, a non-negative pseudo-random number in [0, n) from the default source.
Int63 This function returns a non-negative pseudo-random 63-bit integer as an int64 from the default source.
Int63n This function returns, as an int64, a non-negative pseudo-random number in [0, n) from the default source.
Intn This function returns, as an int, a non-negative pseudo-random number in [0, n) from the default source.
NormFloat64 This function returns a normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution (mean = 0, stddev = 1) from the default source.
Perm This function returns, as a slice of n ints, a pseudo-random permutation of the integers [0, n) from the default source.
Read This function generates len(p) random bytes from the default source and writes them into p.
Seed This function provided seed value to initialize the default source to a deterministic state and if Seed is not called, the generator behaves as if seeded by Seed(1)..
Shuffle This function pseudo-randomizes the order of elements using the default source.
Uint32 This function returns a pseudo-random 32-bit value as a uint32 from the default source.
Uint64 This function returns a pseudo-random 64-bit value as a uint64 from the default source.

类型兰特

Method Description
func New This function returns a new Rand that uses random values from src to generate other random values.
func (*Rand) ExpFloat64 This method is used to return an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution.
func (*Rand) Float32 This method returns, as a float32, a pseudo-random number in [0.0, 1.0).
func (*Rand) Float64 This method returns, as a float64, a pseudo-random number in [0.0, 1.0).
func (*Rand) Int This method returns a non-negative pseudo-random int.
func (*Rand) Int31 This method returns a non-negative pseudo-random 31-bit integer as an int32.
func (*Rand) Int31n This method returns, as an int32, a non-negative pseudo-random number in [0, n).
func (*Rand) Int63 This method returns a non-negative pseudo-random 63-bit integer as an int64.
func (*Rand) Int63n This method returns, as an int64, a non-negative pseudo-random number in [0, n).
func (*Rand) Intn This method returns, as an int, a non-negative pseudo-random number in [0, n).
func (*Rand) NormFloat64 This method is used to return a normally distributed float64 in the range -math.MaxFloat64 through +math.MaxFloat64 inclusive, with standard normal distribution (mean = 0, stddev = 1).
func (*Rand) Perm This method returns, as a slice of n ints, a pseudo-random permutation of the integers [0, n).
func (*Rand) Read This method generates len(p) random bytes and writes them into p.
func (*Rand) Seed This method provided seed value to initialize the generator to a deterministic state.
func (*Rand) Shuffle This method pseudo-randomizes the order of elements.
func (*Rand) Uint32 This method returns a pseudo-random 32-bit value as a uint32.
func (*Rand) Uint64 This method returns a pseudo-random 64-bit value as a uint64.

类型来源

Method Description
func NewSource This function returns a new pseudo-random Source seeded with the given value.
type Source64 It is a Source that can also generate uniformly-distributed pseudo-random uint64 values in the range [0, 1<<64) directly. If a Rand r’s underlying Source s implements Source64, then r.Uint64 returns the result of one call to s.Uint64 instead of making two calls to s.Int63.

类型 Zipf

Method Description
func NewZipf This function returns a Zipf variate generator.
func (*Zipf) Uint64 This method returns a value drawn from the Zipf distribution described by the Zipf object.

示例 1:

// Golang program to illustrate 
// how to Get Intn Type Random  
// Number 
package main 
     
import ( 
    "fmt"
    "math/rand"
) 
     
// Main function 
func main() { 
     
    // Finding random numbers of int type 
    // Using Intn() function 
    res_1 := rand.Intn(7) 
    res_2 := rand.Intn(8) 
    res_3 := rand.Intn(2) 
     
    // Displaying the result 
    fmt.Println("Random Number 1: ", res_1) 
    fmt.Println("Random Number 2: ", res_2) 
    fmt.Println("Random Number 3: ", res_3) 
} 

输出:

Random Number 1:  6
Random Number 2:  7
Random Number 3:  1

示例 2:

// Golang program to illustrate 
// how to get a random number 
package main 
   
import ( 
    "fmt"
    "math/rand"
) 
   
// Main function 
func main() { 
   
    // Finding random numbers of int type 
    // Using Int31() function 
    res_1 := rand.Int31() 
    res_2 := rand.Int31() 
    res_3 := rand.Int31() 
   
    // Displaying the result 
    fmt.Println("Random Number 1: ", res_1) 
    fmt.Println("Random Number 2: ", res_2) 
    fmt.Println("Random Number 3: ", res_3) 
} 

输出:

Random Number 1:  1298498081
Random Number 2:  2019727887
Random Number 3:  1427131847