首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

math/rand

  • import "math/rand"
  • 概述
  • 索引
  • 示例

概述

rand 包 执行伪随机数生成器。

随机数由一个 Source 生成。顶级函数(如 Float64 和 Int )使用默认的共享源,每次运行程序时都会产生确定性的值序列。如果每次运行都需要不同的行为,请使用 Seed 函数初始化默认 Source。默认的 Source 对于多个 goroutine 并发使用是安全的,但由 NewSource 创建的Source不是。

对于适合安全敏感工作的随机数字,请参阅 crypto / rand 软件包。

示例

代码语言:javascript
复制
package main

import (
	"fmt"
	"math/rand"
)

func main() {
	rand.Seed(42) // 试着改变这些数字
	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)

此示例显示* Rand上每种方法的使用。全局函数的使用是一样的,没有接收器。

代码语言:javascript
复制
package main

import (
	"fmt"
	"math/rand"
	"os"
	"text/tabwriter"
)

func main() {
	// 创造并设置生成器.
	// 通常应该使用非固定种子, 如time.Now().UnixNano().
	// 使用固定的种子挥在每次运行中产生相同的输出
	r := rand.New(rand.NewSource(99))

	//这里的tabwriter帮助我们生成对齐的输出。
	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 和Float64 的值在 [0, 1)之中.
	show("Float32", r.Float32(), r.Float32(), r.Float32())
	show("Float64", r.Float64(), r.Float64(), r.Float64())

// ExpFloat64值的平均值为1,但是呈指数衰减。
	show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())

// NormFloat64值的平均值为0,标准差为1。
	show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())

// Int31,Int63和Uint32生成给定宽度的值。
// Int方法(未显示)类似于Int31或Int63
//取决于'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和Int63n将其输出限制为<n。
//他们比使用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生成数字[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)

打包文件

func ExpFloat64(查看源文件)

代码语言:javascript
复制
func ExpFloat64() float64

ExpFloat64 返回指数分布的 float64 ,其范围为 (0, +math.MaxFloat64],其指数分布的速率参数(lambda)为1,平均值为默认 Source 的 1 / lambda(1)。不同的速率参数,调用者可以使用以下方式调整输出:

代码语言:javascript
复制
sample = ExpFloat64() / desiredRateParameter
代码语言:javascript
复制
func Float32() float32

Float32 以默认 Source 的形式返回一个作为 float32 的 [0.0,1.0) 中的伪随机数。

代码语言:javascript
复制
func Float64() float64

Float64 以默认 Source 的形式返回,作为 float64,一个 [0.0,1.0) 中的伪随机数字。

代码语言:javascript
复制
func Int() int

Int 从默认的 Source 返回一个非负的伪随机 int。

代码语言:javascript
复制
func Int31() int32

Int31 从默认 Source 返回一个非负的伪随机31位整数作为 int32。

代码语言:javascript
复制
func Int31n(n int32) int32

Int31n 以默认 Source 的形式返回 [0,n)中的非负伪随机数,作为 int32。如果n <= 0,它会发生混乱。

代码语言:javascript
复制
func Int63() int64

Int63 从默认的 Source 返回一个非负的伪随机的63位整数作为 int64。

代码语言:javascript
复制
func Int63n(n int64) int64

Int63n 以 int64 形式返回来自默认 Source 的 [0,n] 中的非负伪随机数。如果n <= 0,它会发生混乱。

代码语言:javascript
复制
func Intn(n int) int

Intn 以 int 形式返回来自默认 Source 的 [0,n) 中的非负伪随机数。如果 n <= 0,它会发生混乱。

func NormFloat64(查看源文件)

代码语言:javascript
复制
func NormFloat64() float64

NormFloat64 从默认的 Source 返回正态分布的 float64,范围为-math.MaxFloat64,+ math.MaxFloat64,标准正态分布(mean = 0,stddev = 1)。要产生不同的正态分布,调用者可以使用以下方式调整输出:

代码语言:javascript
复制
sample = NormFloat64() * desiredStdDev + desiredMean
代码语言:javascript
复制
func Perm(n int) []int

Perm 以默认 Source 的形式返回整数 [0,n) 的伪随机置换。

示例

代码语言:javascript
复制
package main

import (
	"fmt"
	"math/rand"
)

func main() {
	for _, value := range rand.Perm(3) {
		fmt.Println(value)
	}

}
代码语言:javascript
复制
func Read(p []byte) (n int, err error)

Read 从默认 Source 生成 len(p) 个随机字节,并将它们写入 p。它总是返回 len(p) 和一个空的错误。与 Rand.Read 方法不同,读取对于并发使用是安全的。

代码语言:javascript
复制
func Seed(seed int64)

Seed 使用提供的种子值将默认 Source 初始化为确定性状态。如果种子没有被调用,那么生成器的行为就像种子(1)播种一样。具有相同余数的种子值除以2 ^ 31-1会生成相同的伪随机序列。与 Rand.Seed 方法不同,种子对于并发使用是安全的。

func Uint32Source

代码语言:javascript
复制
func Uint32() uint32

Uint32从默认的 Source 返回一个伪随机的32位值作为 uint32。

func Uint64Source

代码语言:javascript
复制
func Uint64() uint64

Uint64 从默认的 Source 返回一个伪随机的64位值作为 uint64。

type RandSource

Rand 是随机数字的来源。

代码语言:javascript
复制
type Rand struct {
        // contains filtered or unexported fields
}

func NewSource

代码语言:javascript
复制
func New(src Source) *Rand

New 返回一个新的Rand,它使用 src 中的随机值生成其他随机值。

func (*Rand) ExpFloat64Source

代码语言:javascript
复制
func (r *Rand) ExpFloat64() float64

ExpFloat64 返回指数分布的 float64,其范围为 (0, +math.MaxFloat64] ,其指数分布的速率参数(lambda)为1,平均值为 1 / lambda(1)。为了生成具有不同速率参数的分布,呼叫者可以使用以下方式调整输出:

代码语言:javascript
复制
sample = ExpFloat64() / desiredRateParameter

func (*Rand) Float32Source

代码语言:javascript
复制
func (r *Rand) Float32() float32

Float32 作为 float32 返回 [0.0,1.0) 中的伪随机数。

func (*Rand) Float64Source

代码语言:javascript
复制
func (r *Rand) Float64() float64

Float64 作为 float64 返回 [0.0,1.0) 中的伪随机数。

func (*Rand) IntSource

代码语言:javascript
复制
func (r *Rand) Int() int

Int 返回一个非负的伪随机 int。

func (*Rand) Int31Source

代码语言:javascript
复制
func (r *Rand) Int31() int32

Int31 以 int32 形式返回一个非负的伪随机31位整数。

func (*Rand) Int31nSource

代码语言:javascript
复制
func (r *Rand) Int31n(n int32) int32

Int31n 以 int32 形式返回 [0,n)中的非负伪随机数。如果 n <= 0,它会发生混乱。

func (*Rand) Int63Source

代码语言:javascript
复制
func (r *Rand) Int63() int64

Int63 以 int64 的形式返回一个非负的伪随机63位整数。

func (*Rand) Int63nSource

代码语言:javascript
复制
func (r *Rand) Int63n(n int64) int64

Int63n 以 int64 的形式返回 [0,n)中的非负伪随机数。如果 n <= 0,它会发生混乱。

func (*Rand) IntnSource

代码语言:javascript
复制
func (r *Rand) Intn(n int) int

Intn 返回 int [0,n] 中的非负伪随机数。如果 n <= 0,它会发生混乱。

func (*Rand) NormFloat64Source

代码语言:javascript
复制
func (r *Rand) NormFloat64() float64

NormFloat64 返回正态分布的 float64,范围为-math.MaxFloat64,+ math.MaxFloat64,标准正态分布(mean = 0,stddev = 1)。要产生不同的正态分布,呼叫者可以使用以下方式调整输出:

代码语言:javascript
复制
sample = NormFloat64() * desiredStdDev + desiredMean

func (*Rand) PermSource

代码语言:javascript
复制
func (r *Rand) Perm(n int) []int

Perm作为一个 n 分片返回一个整数 [0,n)的伪随机置换。

func (*Rand) ReadSource

代码语言:javascript
复制
func (r *Rand) Read(p []byte) (n int, err error)

Read 生成 len(p)个随机字节并将它们写入p。它总是返回 len(p)和一个零错误。阅读不应与任何其他 Rand 方法同时调用。

func (*Rand) SeedSource

代码语言:javascript
复制
func (r *Rand) Seed(seed int64)

Seed 使用提供的种子值将发生器初始化为确定性状态。Seed 不应该与任何其他 Rand 方法同时调用。

func (*Rand) Uint32Source

代码语言:javascript
复制
func (r *Rand) Uint32() uint32

Uint32 返回一个伪随机的32位值作为 uint32。

func (*Rand) Uint64Source

代码语言:javascript
复制
func (r *Rand) Uint64() uint64

Uint64 返回一个伪随机的64位值作为 uint64。

type SourceSource

源表示均匀分布的伪随机 int64 值的来源,范围 [0,1 << 63)。

代码语言:javascript
复制
type Source interface {
        Int63() int64
        Seed(seed int64)
}

func NewSourceSource

代码语言:javascript
复制
func NewSource(seed int64) Source

NewSource 返回一个新的伪随机源,并给定值。与顶级函数使用的默认源不同,此源对于多个 goroutine 并发使用并不安全。

type Source64Source

Source64是一个Source,它也可以直接在[0,1 << 64)范围内生成均匀分布的伪随机uint64值。如果 Rand r 的底层 Source 实现Source64,则 r.Uint64 将一次调用的结果返回给 s.Uint64,而不是对 s.Int63进行两次调用。

代码语言:javascript
复制
type Source64 interface {
        Source
        Uint64() uint64
}

type ZipfSource

Zipf 生成 Zipf 分布式变量。

代码语言:javascript
复制
type Zipf struct {
        // contains filtered or unexported fields
}

func NewZipfSource

代码语言:javascript
复制
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 (*Zipf) Uint64Source

代码语言:javascript
复制
func (z *Zipf) Uint64() uint64

Uint64 返回从 Zipf 对象描述的 Zipf 分布中绘制的值。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com