random

package
v0.0.0-...-d62e3dc Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func ByteArray

func ByteArray(size int) []byte

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

func Duration

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

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

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

Types

type Coin

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

Coin 硬币,即只有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 any,
	buildFunc DiscreteDistributionCandidateBuildFunc) *DiscreteDistributionCandidate

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

func (DiscreteDistributionCandidate) Context

func (c DiscreteDistributionCandidate) Context() any

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

func (DiscreteDistributionCandidate) Key

Key 返回候选项的键值

func (DiscreteDistributionCandidate) Weight

Weight 返回候选项的权重

type DiscreteDistributionCandidateBuildFunc

type DiscreteDistributionCandidateBuildFunc = func(context any) (int, any)

type IDiscreteDistributionCandidate

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

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

type IDiscreteDistributionDice

type IDiscreteDistributionDice interface {
	Append(c IDiscreteDistributionCandidate) IDiscreteDistributionDice
	Build() (err error)
	Roll() (ctx any, 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