🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# package rand `import "math/rand"` rand包实现了伪随机数生成器。 随机数从资源生成。包水平的函数都使用的默认的公共资源。该资源会在程序每次运行时都产生确定的序列。如果需要每次运行产生不同的序列,应使用Seed函数进行初始化。默认资源可以安全的用于多go程并发。 Example ``` 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))]) ``` Output: ``` Magic 8-Ball says: As I see it yes ``` Example (Rand) ``` // 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.Int63(), r.Int63(), r.Int63()) // 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)) ``` Output: ``` Float32 0.2635776 0.6358173 0.6718283 Float64 0.628605430454327 0.4504798828572669 0.9562755949377957 ExpFloat64 0.3362240648200941 1.4256072328483647 0.24354758816173044 NormFloat64 0.17233959114940064 1.577014951434847 0.04259129641113857 Int31 1501292890 1486668269 182840835 Int63 3546343826724305832 5724354148158589552 5239846799706671610 Uint32 5927547564735367388 637072299495207830 4128311955958246186 Intn(10) 1 2 5 Int31n(10) 4 7 8 Int63n(10) 7 6 3 Perm [1 4 2 3 0] [4 2 1 3 0] [1 2 4 0 3] ``` ## Index * [type Source](#Source) * [func NewSource(seed int64) Source](#NewSource) * [type Rand](#Rand) * [func New(src Source) \*Rand](#New) * [func (r \*Rand) Seed(seed int64)](#Rand.Seed) * [func (r \*Rand) Int() int](#Rand.Int) * [func (r \*Rand) Int31() int32](#Rand.Int31) * [func (r \*Rand) Int63() int64](#Rand.Int63) * [func (r \*Rand) Uint32() uint32](#Rand.Uint32) * [func (r \*Rand) Intn(n int) int](#Rand.Intn) * [func (r \*Rand) Int31n(n int32) int32](#Rand.Int31n) * [func (r \*Rand) Int63n(n int64) int64](#Rand.Int63n) * [func (r \*Rand) Float32() float32](#Rand.Float32) * [func (r \*Rand) Float64() float64](#Rand.Float64) * [func (r \*Rand) NormFloat64() float64](#Rand.NormFloat64) * [func (r \*Rand) ExpFloat64() float64](#Rand.ExpFloat64) * [func (r \*Rand) Perm(n int) []int](#Rand.Perm) * [type Zipf](#Zipf) * [func NewZipf(r \*Rand, s float64, v float64, imax uint64) \*Zipf](#NewZipf) * [func (z \*Zipf) Uint64() uint64](#Zipf.Uint64) * [func Seed(seed int64)](#Seed) * [func Int() int](#Int) * [func Int31() int32](#Int31) * [func Int63() int64](#Int63) * [func Uint32() uint32](#Uint32) * [func Intn(n int) int](#Intn) * [func Int31n(n int32) int32](#Int31n) * [func Int63n(n int64) int64](#Int63n) * [func Float32() float32](#Float32) * [func Float64() float64](#Float64) * [func NormFloat64() float64](#NormFloat64) * [func ExpFloat64() float64](#ExpFloat64) * [func Perm(n int) []int](#Perm) ### Examples * [package](#example-package) * [package (Rand)](#example-package--Rand) ## type [Source](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L18 "View Source") ``` type Source interface { Int63() int64 Seed(seed int64) } ``` Source代表一个生成均匀分布在范围[0, 1&lt;&lt;63)的int64值的(伪随机的)资源。 ### func [NewSource](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L24 "View Source") ``` func NewSource(seed int64) Source ``` 使用给定的种子创建一个伪随机资源。 ## type [Rand](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L31 "View Source") ``` type Rand struct { // 内含隐藏或非导出字段 } ``` Rand生成服从多种分布的随机数。 ### func [New](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L37 "View Source") ``` func New(src Source) *Rand ``` 返回一个使用src生产的随机数来生成其他各种分布的随机数值的\*Rand。 ### func (\*Rand) [Seed](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L40 "View Source") ``` func (r *Rand) Seed(seed int64) ``` 使用给定的seed来初始化生成器到一个确定的状态。 ### func (\*Rand) [Int](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L52 "View Source") ``` func (r *Rand) Int() int ``` 返回一个非负的伪随机int值。 ### func (\*Rand) [Int31](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L49 "View Source") ``` func (r *Rand) Int31() int32 ``` 返回一个int32类型的非负的31位伪随机数。 ### func (\*Rand) [Int63](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L43 "View Source") ``` func (r *Rand) Int63() int64 ``` 返回一个int64类型的非负的63位伪随机数。 ### func (\*Rand) [Uint32](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L46 "View Source") ``` func (r *Rand) Uint32() uint32 ``` 返回一个uint32类型的非负的32位伪随机数。 ### func (\*Rand) [Intn](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L93 "View Source") ``` func (r *Rand) Intn(n int) int ``` 返回一个取值范围在[0,n)的伪随机int值,如果n&lt;=0会panic。 ### func (\*Rand) [Int31n](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L76 "View Source") ``` func (r *Rand) Int31n(n int32) int32 ``` 返回一个取值范围在[0,n)的伪随机int32值,如果n&lt;=0会panic ### func (\*Rand) [Int63n](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L59 "View Source") ``` func (r *Rand) Int63n(n int64) int64 ``` 返回一个取值范围在[0,n)的伪随机int64值,如果n&lt;=0会panic。 ### func (\*Rand) [Float32](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L131 "View Source") ``` func (r *Rand) Float32() float32 ``` 返回一个取值范围在[0.0, 1.0)的伪随机float32值。 ### func (\*Rand) [Float64](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L104 "View Source") ``` func (r *Rand) Float64() float64 ``` 返回一个取值范围在[0.0, 1.0)的伪随机float64值。 ### func (\*Rand) [NormFloat64](https://github.com/golang/go/blob/master/src/math/rand/normal.go#L38 "View Source") ``` func (r *Rand) NormFloat64() float64 ``` 返回一个服从标准正态分布(标准差=1,期望=0)、取值范围在[-math.MaxFloat64, +math.MaxFloat64]的float64值 如果要生成不同的正态分布值,调用者可用如下代码调整输出: ``` sample = NormFloat64() * 标准差 + 期望 ``` ### func (\*Rand) [ExpFloat64](https://github.com/golang/go/blob/master/src/math/rand/exp.go#L31 "View Source") ``` func (r *Rand) ExpFloat64() float64 ``` 返回一个服从标准指数分布(率参数=1,率参数是期望的倒数)、取值范围在(0, +math.MaxFloat64]的float64值 如要生成不同的指数分布值,调用者可用如下代码调整输出: ``` sample = ExpFloat64() / 率参数 ``` ### func (\*Rand) [Perm](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L146 "View Source") ``` func (r *Rand) Perm(n int) []int ``` 返回一个有n个元素的,[0,n)范围内整数的伪随机排列的切片。 ## type [Zipf](https://github.com/golang/go/blob/master/src/math/rand/zipf.go#L15 "View Source") ``` type Zipf struct { // 内含隐藏或非导出字段 } ``` Zipf生成服从齐普夫分布的随机数。 ### func [NewZipf](https://github.com/golang/go/blob/master/src/math/rand/zipf.go#L37 "View Source") ``` func NewZipf(r *Rand, s float64, v float64, imax uint64) *Zipf ``` NewZipf返回一个[0, imax]范围内的齐普夫随机数生成器。 齐普夫分布:值k出现的几率p(k)正比于(v+k)**(-s),其中s&gt;1且k&gt;=0且v&gt;=1。 ### func (\*Zipf) [Uint64](https://github.com/golang/go/blob/master/src/math/rand/zipf.go#L56 "View Source") ``` func (z *Zipf) Uint64() uint64 ``` Uint64返回一个服从Zipf对象描述的齐普夫分布的随机数。 ## func [Seed](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L165 "View Source") ``` func Seed(seed int64) ``` 使用给定的seed将默认资源初始化到一个确定的状态;如未调用Seed,默认资源的行为就好像调用了Seed(1)。 ## func [Int](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L180 "View Source") ``` func Int() int ``` 返回一个非负的伪随机int值。 ## func [Int31](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L177 "View Source") ``` func Int31() int32 ``` 返回一个int32类型的非负的31位伪随机数。 ## func [Int63](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L169 "View Source") ``` func Int63() int64 ``` 返回一个int64类型的非负的63位伪随机数。 ## func [Uint32](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L173 "View Source") ``` func Uint32() uint32 ``` 返回一个uint32类型的非负的32位伪随机数。 ## func [Intn](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L195 "View Source") ``` func Intn(n int) int ``` 返回一个取值范围在[0,n)的伪随机int值,如果n&lt;=0会panic。 ## func [Int31n](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L190 "View Source") ``` func Int31n(n int32) int32 ``` 返回一个取值范围在[0,n)的伪随机int32值,如果n&lt;=0会panic。 ## func [Int63n](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L185 "View Source") ``` func Int63n(n int64) int64 ``` 返回一个取值范围在[0, n)的伪随机int64值,如果n&lt;=0会panic。 ## func [Float32](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L203 "View Source") ``` func Float32() float32 ``` 返回一个取值范围在[0.0, 1.0)的伪随机float32值。 ## func [Float64](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L199 "View Source") ``` func Float64() float64 ``` 返回一个取值范围在[0.0, 1.0)的伪随机float64值。 ## func [NormFloat64](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L218 "View Source") ``` func NormFloat64() float64 ``` 返回一个服从标准正态分布(标准差=1,期望=0)、取值范围在[-math.MaxFloat64, +math.MaxFloat64]的float64值 如果要生成不同的正态分布值,调用者可用如下代码调整输出: ``` sample = NormFloat64() * 标准差 + 期望 ``` ## func [ExpFloat64](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L228 "View Source") ``` func ExpFloat64() float64 ``` 返回一个服从标准指数分布(率参数=1,率参数是期望的倒数)、取值范围在(0, +math.MaxFloat64]的float64值 如要生成不同的指数分布值,调用者可用如下代码调整输出: ``` sample = ExpFloat64() / 率参数 ``` ## func [Perm](https://github.com/golang/go/blob/master/src/math/rand/rand.go#L207 "View Source") ``` func Perm(n int) []int ``` 返回一个有n个元素的,[0,n)范围内整数的伪随机排列的切片。