random

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Strings = NewBytes([]byte("0123456789abcdefghijklmnopqrstuvwxyz"))

Strings 默认的随机字符串生成器,使用 crypto/rand 作为熵源,适合生成 token/secret。 注意:游戏概率类随机请用 Roll / Probability / Relative(math/rand)。

Functions

func Probability

func Probability(per int32, args ...int32) bool

Probability 独立概率,args单位,默认万分比,,,百分比传入100

func Relative

func Relative(items map[int32]int32) int32

Relative 相对概率,权重

func RelativeMulti

func RelativeMulti(items map[int32]int32, num int32, repeat ...bool) []int32

RelativeMulti 相对概率,权重,返回多个。repeat 是否可以重复。 不修改传入的 items map(内部拷贝后操作)。

func Roll

func Roll(a, b int32) int32

Types

type Bytes added in v1.1.0

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

Bytes 基于自定义字符集 + crypto/rand 的安全随机字符串生成器。

func NewBytes added in v1.1.0

func NewBytes(bytes []byte) *Bytes

func (*Bytes) New added in v1.1.0

func (this *Bytes) New(l int) []byte

New 返回长度为 l 的随机字节串,每个字节来自字符集。 使用 crypto/rand + 拒绝采样消除模偏差。 单次分配,原地处理:读 l 字节后就地筛选,极少情况需要补读。

func (*Bytes) String added in v1.1.0

func (this *Bytes) String(l int) string

type Data added in v1.0.7

type Data struct {
	Key int32 //序号
	Val int32 //权重
}

func (*Data) GetKey added in v1.0.7

func (this *Data) GetKey() int32

func (*Data) GetVal added in v1.0.7

func (this *Data) GetVal() int32

type Less added in v1.0.7

type Less func(a, b *Data) bool

type Random

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

func New

func New(items map[int32]int32, less ...Less) *Random

func (*Random) Add added in v1.1.0

func (this *Random) Add(k, v int32)

Add 添加备选项 全部添加完毕后需要手动调用Sort排序才能使用

func (*Random) Filter added in v1.0.7

func (this *Random) Filter(filter func(k, v int32) bool, less ...Less) *Random

Filter 根据 filter 剔除不符合规则的项目

func (*Random) Less added in v1.1.0

func (this *Random) Less(i, j *Data) bool

func (*Random) Multi

func (this *Random) Multi(num int) (r []int32)

Multi 随机多个不重复 用 bitmap 标记已选中项,避免拷贝 items 切片和分配新 Data

func (*Random) Probability added in v1.2.0

func (this *Random) Probability(args ...int32) (r []int32)

Probability 按独立概率计算每一组的命中

func (*Random) Range added in v1.0.6

func (this *Random) Range(f func(k, v int32) bool)

func (*Random) Reset added in v1.0.7

func (this *Random) Reset(filter func(k, v int32) int32, less ...Less) *Random

Reset 根据 filter 重新设定权重

func (*Random) Roll

func (this *Random) Roll() int32

Roll 简单的权重算法,直接计算区间落点

func (*Random) Sort added in v1.0.7

func (this *Random) Sort(less ...Less) *Random

func (*Random) Weight added in v1.0.6

func (this *Random) Weight() (r int32)

Jump to

Keyboard shortcuts

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