# 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<<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<=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<=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<=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>1且k>=0且v>=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<=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<=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<=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)范圍內整數的偽隨機排列的切片。
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe