?
This document uses PHP Chinese website manual Release
import "math/rand"
概觀
索引
示例
打包rand 執(zhí)行偽隨機(jī)數(shù)生成器。
隨機(jī)數(shù)由一個 Source 生成。頂級函數(shù)(如 Float64 和 Int )使用默認(rèn)的共享源,每次運(yùn)行程序時都會產(chǎn)生確定性的值序列。如果每次運(yùn)行都需要不同的行為,請使用 Seed 函數(shù)初始化默認(rèn) Source。默認(rèn)的 Source 對于多個 goroutine 并發(fā)使用是安全的,但由 NewSource 創(chuàng)建的源不是。
對于適合安全敏感工作的隨機(jī)數(shù)字,請參閱 crypto / rand 軟件包。
package mainimport ("fmt""math/rand")func main() { rand.Seed(42) // Try changing this number! answers := []string{"It is certain","It is decidedly so","Without a doubt","Yes definitely","You may rely on it","As I see it yes","Most likely","Outlook good","Yes","Signs point to yes","Reply hazy try again","Ask again later","Better not tell you now","Cannot predict now","Concentrate and ask again","Don't count on it","My reply is no","My sources say no","Outlook not so good","Very doubtful",} fmt.Println("Magic 8-Ball says:", answers[rand.Intn(len(answers))])}
此示例顯示* Rand上每種方法的使用。全局功能的使用是一樣的,沒有接收器。
package mainimport ("fmt""math/rand""os""text/tabwriter")func main() {// Create and seed the generator.// Typically a non-fixed seed should be used, such as time.Now().UnixNano().// Using a fixed seed will produce the same output on every run. r := rand.New(rand.NewSource(99))// The tabwriter here helps us generate aligned output. w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) defer w.Flush() show := func(name string, v1, v2, v3 interface{}) { fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3)}// Float32 and Float64 values are in [0, 1).show("Float32", r.Float32(), r.Float32(), r.Float32())show("Float64", r.Float64(), r.Float64(), r.Float64())// ExpFloat64 values have an average of 1 but decay exponentially.show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())// NormFloat64 values have an average of 0 and a standard deviation of 1.show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())// Int31, Int63, and Uint32 generate values of the given width.// The Int method (not shown) is like either Int31 or Int63// depending on the size of 'int'.show("Int31", r.Int31(), r.Int31(), r.Int31())show("Int63", r.Int63(), r.Int63(), r.Int63())show("Uint32", r.Uint32(), r.Uint32(), r.Uint32())// Intn, Int31n, and Int63n limit their output to be < n.// They do so more carefully than using r.Int()%n.show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10))show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10))show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10))// Perm generates a random permutation of the numbers [0, n).show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))}
func ExpFloat64() float64
func Float32() float32
func Float64() float64
func Int() int
func Int31() int32
func Int31n(n int32) int32
func Int63() int64
func Int63n(n int64) int64
func Intn(n int) int
func NormFloat64() float64
func Perm(n int) []int
func Read(p []byte) (n int, err error)
func Seed(seed int64)
func Uint32() uint32
func Uint64() uint64
type Rand
func New(src Source) *Rand
func (r *Rand) ExpFloat64() float64
func (r *Rand) Float32() float32
func (r *Rand) Float64() float64
func (r *Rand) Int() int
func (r *Rand) Int31() int32
func (r *Rand) Int31n(n int32) int32
func (r *Rand) Int63() int64
func (r *Rand) Int63n(n int64) int64
func (r *Rand) Intn(n int) int
func (r *Rand) NormFloat64() float64
func (r *Rand) Perm(n int) []int
func (r *Rand) Read(p []byte) (n int, err error)
func (r *Rand) Seed(seed int64)
func (r *Rand) Uint32() uint32
func (r *Rand) Uint64() uint64
type Source
func NewSource(seed int64) Source
type Source64
type Zipf
func NewZipf(r *Rand, s float64, v float64, imax uint64) *Zipf
func (z *Zipf) Uint64() uint64
Package Perm Package (Rand)
exp.go normal.go rand.go rng.go zipf.go
func ExpFloat64() float64
ExpFloat64 返回指數(shù)分布的 float64 ,其范圍為 (0, +math.MaxFloat64],其指數(shù)分布的速率參數(shù)(lambda)為1,平均值為默認(rèn) Source 的 1 / lambda(1)。不同的速率參數(shù),呼叫者可以使用以下方式調(diào)整輸出:
sample = ExpFloat64() / desiredRateParameter
func Float32() float32
Float32 以默認(rèn) Source 的形式返回一個作為 float32 的 [0.0,1.0) 中的偽隨機(jī)數(shù)。
func Float64() float64
Float64 以默認(rèn) Source 的形式返回,作為 float64,一個 [0.0,1.0) 中的偽隨機(jī)數(shù)字。
func Int() int
Int 從默認(rèn)的 Source 返回一個非負(fù)的偽隨機(jī) int。
func Int31() int32
Int31 從默認(rèn) Source 返回一個非負(fù)的偽隨機(jī)31位整數(shù)作為 int32。
func Int31n(n int32) int32
Int31n 以默認(rèn) Source 的形式返回 [0,n)中的非負(fù)偽隨機(jī)數(shù),作為 int32。如果n <= 0,它會發(fā)生混亂。
func Int63() int64
Int63 從默認(rèn)的 Source 返回一個非負(fù)的偽隨機(jī)的63位整數(shù)作為 int64。
func Int63n(n int64) int64
Int63n 以 int64 形式返回來自默認(rèn) Source 的 [0,n] 中的非負(fù)偽隨機(jī)數(shù)。如果n <= 0,它會發(fā)生混亂。
func Intn(n int) int
Intn 以 int 形式返回來自默認(rèn) Source 的 [0,n] 中的非負(fù)偽隨機(jī)數(shù)。如果 n <= 0,它會發(fā)生混亂。
func NormFloat64() float64
NormFloat64 從默認(rèn)的 Source 返回正態(tài)分布的 float64,范圍為-math.MaxFloat64,+ math.MaxFloat64,標(biāo)準(zhǔn)正態(tài)分布(mean = 0,stddev = 1)。要產(chǎn)生不同的正態(tài)分布,調(diào)用者可以使用以下方式調(diào)整輸出:
sample = NormFloat64() * desiredStdDev + desiredMean
func Perm(n int) []int
Perm 以默認(rèn) Source 的形式返回整數(shù) [0,n) 的偽隨機(jī)置換。
package mainimport ("fmt""math/rand")func main() {for _, value := range rand.Perm(3) { fmt.Println(value)}}
func Read(p []byte) (n int, err error)
Read 從默認(rèn) Source 生成 len(p) 個隨機(jī)字節(jié),并將它們寫入 p。它總是返回 len(p) 和一個零錯誤。與 Rand.Read 方法不同,讀取對于并發(fā)使用是安全的。
func Seed(seed int64)
Seed 使用提供的種子值將默認(rèn) Source 初始化為確定性狀態(tài)。如果種子沒有被調(diào)用,那么生成器的行為就像種子(1)播種一樣。具有相同余數(shù)的種子值除以2 ^ 31-1會生成相同的偽隨機(jī)序列。與 Rand.Seed 方法不同,種子對于并發(fā)使用是安全的。
func Uint32() uint32
Uint32從默認(rèn)的 Source 返回一個偽隨機(jī)的32位值作為 uint32。
func Uint64() uint64
Uint64 從默認(rèn)的 Source 返回一個偽隨機(jī)的64位值作為 uint64。
Rand 是隨機(jī)數(shù)字的來源。
type Rand struct { // contains filtered or unexported fields}
func New(src Source) *Rand
New 返回一個新的Rand,它使用 src 中的隨機(jī)值生成其他隨機(jī)值。
func (r *Rand) ExpFloat64() float64
ExpFloat64 返回指數(shù)分布的 float64,其范圍為 (0, +math.MaxFloat64] ,其指數(shù)分布的速率參數(shù)(lambda)為1,平均值為 1 / lambda(1)。為了生成具有不同速率參數(shù)的分布,呼叫者可以使用以下方式調(diào)整輸出:
sample = ExpFloat64() / desiredRateParameter
func (r *Rand) Float32() float32
Float32 作為 float32 返回 [0.0,1.0) 中的偽隨機(jī)數(shù)。
func (r *Rand) Float64() float64
Float64 作為 float64 返回 [0.0,1.0) 中的偽隨機(jī)數(shù)。
func (r *Rand) Int() int
Int 返回一個非負(fù)的偽隨機(jī) int。
func (r *Rand) Int31() int32
Int31 以 int32 形式返回一個非負(fù)的偽隨機(jī)31位整數(shù)。
func (r *Rand) Int31n(n int32) int32
Int31n 以 int32 形式返回 [0,n)中的非負(fù)偽隨機(jī)數(shù)。如果 n <= 0,它會發(fā)生混亂。
func (r *Rand) Int63() int64
Int63 以 int64 的形式返回一個非負(fù)的偽隨機(jī)63位整數(shù)。
func (r *Rand) Int63n(n int64) int64
Int63n 以 int64 的形式返回 [0,n)中的非負(fù)偽隨機(jī)數(shù)。如果 n <= 0,它會發(fā)生混亂。
func (r *Rand) Intn(n int) int
Intn 返回 int [0,n] 中的非負(fù)偽隨機(jī)數(shù)。如果 n <= 0,它會發(fā)生混亂。
func (r *Rand) NormFloat64() float64
NormFloat64 返回正態(tài)分布的 float64,范圍為-math.MaxFloat64,+ math.MaxFloat64,標(biāo)準(zhǔn)正態(tài)分布(mean = 0,stddev = 1)。要產(chǎn)生不同的正態(tài)分布,呼叫者可以使用以下方式調(diào)整輸出:
sample = NormFloat64() * desiredStdDev + desiredMean
func (r *Rand) Perm(n int) []int
Perm作為一個 n 分片返回一個整數(shù) [0,n)的偽隨機(jī)置換。
func (r *Rand) Read(p []byte) (n int, err error)
Read 生成 len(p)個隨機(jī)字節(jié)并將它們寫入p。它總是返回 len(p)和一個零錯誤。閱讀不應(yīng)與任何其他 Rand 方法同時調(diào)用。
func (r *Rand) Seed(seed int64)
Seed 使用提供的種子值將發(fā)生器初始化為確定性狀態(tài)。Seed 不應(yīng)該與任何其他 Rand 方法同時調(diào)用。
func (r *Rand) Uint32() uint32
Uint32 返回一個偽隨機(jī)的32位值作為 uint32。
func (r *Rand) Uint64() uint64
Uint64 返回一個偽隨機(jī)的64位值作為 uint64。
源表示均勻分布的偽隨機(jī) int64 值的來源,范圍 [0,1 << 63)。
type Source interface { Int63() int64 Seed(seed int64)}
func NewSource(seed int64) Source
NewSource 返回一個新的偽隨機(jī)源,并給定值。與頂級函數(shù)使用的默認(rèn)源不同,此源對于多個 goroutine 并發(fā)使用并不安全。
Source64是一個Source,它也可以直接在[0,1 << 64)范圍內(nèi)生成均勻分布的偽隨機(jī)uint64值。如果 Rand r 的底層 Source 實(shí)現(xiàn)Source64,則 r.Uint64 將一次調(diào)用的結(jié)果返回給 s.Uint64,而不是對 s.Int63進(jìn)行兩次調(diào)用。
type Source64 interface { Source Uint64() uint64}
Zipf 生成 Zipf 分布式變量。
type Zipf struct { // contains filtered or unexported fields}
func NewZipf(r *Rand, s float64, v float64, imax uint64) *Zipf
NewZipf 返回一個 Zipf 變量生成器。生成器生成值 k∈0,imax,使得P(k)與(v + k)**(-s)成比例。要求:s> 1且v> = 1。
func (z *Zipf) Uint64() uint64
Uint64 返回從 Zipf 對象描述的 Zipf 分布中繪制的值。