random

package
v0.0.0-...-83fd405 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCandidateIsEmpty = errors.New("candidate is empty")
View Source
var ErrInvalidRate = errors.New("invalid rate, must > 0 and <= 1")
View Source
var ErrNotHit = errors.New("not hit")
View Source
var ErrRawRatesIsEmpty = errors.New("raw randomRates is empty")
View Source
var ErrTotalWeightsIsZero = errors.New("total weights is 0")
View Source
var ErrWeightsIsEmpty = errors.New("weights is empty")

Functions

func ByteArray

func ByteArray(size int) []byte

生成一定长度的随机字节数组

func GenRandomRatesInt

func GenRandomRatesInt(weights map[int]int) (rates map[int]float64, err error)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// GenRandomRatesInt 根据整型权重值生成浮点权重值

func Int

func Int(min int, max int) int

func Int32

func Int32(min int32, max int32) int32

func Int64

func Int64(min int64, max int64) int64

func NormalDiscreteDistributionInt

func NormalDiscreteDistributionInt(fixRates map[int]float64, randomRates map[int]float64) (map[int]float64, int, error)

NormalDiscreteDistributionInt 键值是int的正态权重随机

func NormalDistributionRandom

func NormalDistributionRandom(mu float64, sigma float64) float64

func String

func String(size int) string

生成一定长度的随机字符串

func TimeDuration

func TimeDuration(min time.Duration, max time.Duration) time.Duration

Types

type Coin

type Coin struct {
	// contains filtered or unexported fields
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 硬币,即只有2面的骰子

func NewCoin

func NewCoin(rate float64) (c *Coin, err error)

func (*Coin) Roll

func (c *Coin) Roll() bool

type DiscreteDistributionCandidate

type DiscreteDistributionCandidate struct {
	// contains filtered or unexported fields
}

DiscreteDistributionCandidate 按概率分布随机骰子候选项代理类

func NewDiscreteDistributionCandidate

func NewDiscreteDistributionCandidate(
	context interface{},
	buildFunc DiscreteDistributionCandidateBuildFunc) *DiscreteDistributionCandidate

NewDiscreteDistributionCandidate 构造权重随机骰子候选项

func (DiscreteDistributionCandidate) Context

func (c DiscreteDistributionCandidate) Context() interface{}

Context 返回按概率分布随机骰子候选项的上下文

func (DiscreteDistributionCandidate) Key

func (c DiscreteDistributionCandidate) Key() interface{}

Key 返回候选项的键值

func (DiscreteDistributionCandidate) Weight

Weight 返回候选项的权重

type DiscreteDistributionCandidateBuildFunc

type DiscreteDistributionCandidateBuildFunc = func(context interface{}) (int, interface{})

type IDiscreteDistributionCandidate

type IDiscreteDistributionCandidate interface {
	Context() interface{}
	Weight() int
	Key() interface{}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IDiscreteDistributionCandidate 按概率分布随机骰子候选项接口

type IDiscreteDistributionDice

type IDiscreteDistributionDice interface {
	Append(c IDiscreteDistributionCandidate) IDiscreteDistributionDice
	Build() (err error)
	Roll() (ctx interface{}, err error)
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

func NewDiscreteDistributionDice

func NewDiscreteDistributionDice() IDiscreteDistributionDice

NewDiscreteDistributionDice 构造一个按概率分布随机骰子

func NewNormalDiscreteDistributionIntDice

func NewNormalDiscreteDistributionIntDice() IDiscreteDistributionDice

NewNormalDiscreteDistributionIntDice 构造一个键值是int的正态权重随机骰子

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL