rands

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 2 Imported by: 3

README

rands Build Status license codecov Go Reference

rands 为一个随机字符串生成工具。

// 生成一个长度为 [8,10) 之间的随机字符串
str := rands.String(8, 10, []byte("1234567890abcdefg"))


// 生成一个带缓存功能的随机字符串生成器
r, err := rands.New(time.Now().Unix(), 100, 5, 7, []byte("asdfghijklmn"))
str1 := r.String()
str2 := r.String()

安装

go get github.com/issue9/rands

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package rands 生成各种随机字符串

// 生成一个长度介于 [6,9) 之间的随机字符串
str := rands.String(6, 9, "1343567")

// 生成一个带缓存功能的随机字符串生成器
r := rands.New(time.Now().Unix(), 100, 5, 7, "adbcdefgadf;dfe1334")
str1 := r.String()
str2 := r.String()

NOTE: 仅是随机字符串,不保证唯一性。

Index

Constants

This section is empty.

Variables

View Source
var (
	Alpha                  = []byte(alpha)
	Number                 = []byte(number)
	Punctuation            = []byte(punctuation)
	AlphaNumber            = []byte(alphaNumber)
	AlphaNumberPunctuation = []byte(alphaNumberPunctuation)
)

提供几种常见的随机字符组合方式

Functions

func Bytes

func Bytes(min, max int, bs []byte) []byte

Bytes 产生随机字符数组

其长度为[min, max),bs 所有的随机字符串从此处取。

func Seed

func Seed(seed int64)

Seed 手动指定一个随机种子

默认情况下使用当前包初始化时的时间戳作为随机种子。 BytesString 依赖此项。但是 Rands 有专门的随机函数,不受此影响。

func String

func String(min, max int, bs []byte) string

String 产生一个随机字符串

其长度为[min, max),bs 可用的随机字符串。

Types

type Rands

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

Rands 提供随机字符串的生成

func New

func New(seed int64, bufferSize, min, max int, bs []byte) *Rands

New 声明一个 Rands 变量

seed 随机种子,若为 0 表示使用当前时间作为随机种子。 bufferSize 缓存的随机字符串数量,若为 0,表示不缓存。

func (*Rands) Bytes

func (r *Rands) Bytes() []byte

Bytes 产生随机字符数组

功能与全局函数 Bytes 相同,但参数通过 New 预先指定。

func (*Rands) Seed

func (r *Rands) Seed(seed int64)

Seed 重新指定随机种子

func (*Rands) Stop

func (r *Rands) Stop()

Stop 停止产生随机字符串

如果 New 的 bufferSize 为零,则此方法可以不调用。 否则需要此方法关闭随机字符串的生成。

func (*Rands) String

func (r *Rands) String() string

String 产生一个随机字符串

功能与全局函数 String 相同,但参数通过 New 预先指定。

Jump to

Keyboard shortcuts

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