概述
Golang的版本信息
go version go1.13.4 darwin/amd64
簡(jiǎn)介
Package rand implements pseudo-random number generators.
Random numbers are generated by a Source. Top-level functions, such as Float64 and Int, use a default shared Source that produces a deterministic sequence of values each time a program is run. Use the Seed function to initialize the default Source if different behavior is required for each run. The default Source is safe for concurrent use by multiple goroutines, but Sources created by NewSource are not.
Mathematical interval notation such as [0, n) is used throughout the documentation for this package.
For random numbers suitable for security-sensitive work, see the crypto/rand package.
rand
包實(shí)現(xiàn)了偽隨機(jī)數(shù)生成器睹簇。隨機(jī)數(shù)由源(Source)生成,頂級(jí)函數(shù),如Float64
和Int
使用一個(gè)默認(rèn)的共享源朗若,該源在每次運(yùn)行程序時(shí)都會(huì)產(chǎn)生確定的值序列。如果每次運(yùn)行時(shí)需要不同的行為昌罩,可以使用Seed
函數(shù)初始化默認(rèn)源哭懈。默認(rèn)的Source
可安全地供多個(gè)goroutine
并發(fā)使用,但不適用于用NewSource
函數(shù)創(chuàng)建的Source
茎用。有關(guān)適合于安全敏感工作的真隨機(jī)數(shù)遣总,請(qǐng)參考crypto/rand
軟件包。
-- | 隨機(jī)性 | 不可預(yù)測(cè)性 | 不可重現(xiàn)性 | 用途 | 備注 |
---|---|---|---|---|---|
弱偽隨機(jī)數(shù) | 有 | 沒有 | 沒有 | 不可用于密碼技術(shù) | 不帶種子函數(shù)時(shí)調(diào)用math/rand
|
強(qiáng)偽隨機(jī)數(shù) | 有 | 有 | 沒有 | 可用于密碼技術(shù) | 帶種子函數(shù)時(shí)調(diào)用math/rand
|
真隨機(jī)數(shù) | 有 | 有 | 有 | 可用于密碼技術(shù) | 直接調(diào)用crypto/rand
|
源碼解析
源碼包目錄解析
rand
│ example_test.go
│ exp.go
│ gen_cooked.go
│ normal.go
│ race_test.go
│ rand.go
│ rand_test.go
│ regress_test.go
│ rng.go
│ zipf.go