random

package
v0.11.24 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2024 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2023-07-28 00:50:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-13 18:15:29 * @FilePath: \go-toolbox\pkg\random\model.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2023-07-28 00:50:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-17 01:10:01 * @FilePath: \go-toolbox\pkg\random\rand.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

Index

Constants

View Source
const (
	// 自定义数字区间
	DEC_BYTES = "0123456789"
	// 自定义hex区间
	HEX_BYTES = "ABCDEF0123456789"
	// 自定义字符区间
	ALPHA_BYTES  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	LETTER_BYTES = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

	LETTER_IDX_BITS = 6                      // 6 bits to represent a letter index
	LETTER_IDX_MASK = 1<<LETTER_IDX_BITS - 1 // All 1-bits, as many as letterIdxBits
	LETTER_IDX_MAX  = 31 / LETTER_IDX_BITS   // # of letter indices fitting in 31 bits
)

Variables

This section is empty.

Functions

func FRandAlphaBytes

func FRandAlphaBytes(n int) []byte

FRandAlphaBytes generates random alpha bytes.

func FRandAlphaString

func FRandAlphaString(n int) string

FRandAlphaString 指定长度的随机字母字符串

func FRandBool

func FRandBool() bool

生成随机布尔值

func FRandBytes

func FRandBytes(n int) []byte

FRandBytes random bytes, but faster.

func FRandBytesJSON

func FRandBytesJSON(length int) (string, error)

FRandBytesJSON 生成指定长度的随机字节字符串,并返回 JSON 格式

func FRandBytesLetters

func FRandBytesLetters(n int, letters string) []byte

FRandBytesLetters 生成指定长度的字符切片

func FRandDecBytes

func FRandDecBytes(n int) []byte

RandDecBytes 指定长度的随机数字切片

func FRandDecString

func FRandDecString(n int) string

FRandDecString 指定长度的随机数字字符串

func FRandHexBytes

func FRandHexBytes(n int) []byte

FRandHexBytes generates random hexadecimal bytes.

func FRandHexString

func FRandHexString(n int) string

FRandHexString 指定长度的随机 hex 字符串

func FRandInt

func FRandInt(min, max int) int

FRandInt (>=)min - (<)max

func FRandString

func FRandString(n int) string

FRandString a random string, which may contain uppercase letters, lowercase letters and numbers. Ref: stackoverflow.icza

func FRandTime

func FRandTime() time.Time

生成随机时间

func FRandUint32

func FRandUint32(min, max uint32) uint32

FRandUint32 (>=)min - (<)max

func FastIntn

func FastIntn(n int) int

FastIntn this is similar to rand.Intn, but faster. A non-negative pseudo-random number in the half-open interval [0,n). Return 0 if n <= 0.

func FastRand

func FastRand() uint32

FastRand 随机数

func FastRand64

func FastRand64() uint64

func FastRandn

func FastRandn(n uint32) uint32

FastRandn 等同于 FastRandn() % n, 但更快 See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/

func FastRandu

func FastRandu() uint

func GenerateAvailablePort added in v0.11.10

func GenerateAvailablePort(ports ...int) (int, error)

GenerateAvailablePort 返回一个随机的可用端口号 如果未提供ports参数或提供的参数长度不为2,则使用默认端口范围1024到65535

func GenerateRandModel added in v0.11.9

func GenerateRandModel(model interface{}) (interface{}, string, error)

GenerateRandModel 生成随机模型的 JSON 格式

func NewRand

func NewRand(seed ...int64) *rand.Rand

NewRand goroutine-safe rand.Rand, optional seed value

func RandFloat

func RandFloat(min, max float64) (v float64)

RandFloat *

  • @Description: 随机小数
  • @param min
  • @param max
  • @return v

func RandHex added in v0.11.9

func RandHex(bytesLen int, customBytes ...string) string

RandHex 随机一个hex字符串

func RandInt

func RandInt(min, max int) (v int)

RandInt *

  • @Description: 随机整数
  • @param start
  • @param end
  • @return v

func RandNumber added in v0.11.9

func RandNumber(length int, customBytes ...string) string

RandNumber 随机一个数字字符串

func RandNumericalLargeSlice added in v0.11.9

func RandNumericalLargeSlice[T types.Numerical](largeSize ...int) []T

RandNumericalLargeSlice 随机生成大数据整数切片

func RandString

func RandString(n int, mode RandType) (str string)

RandString *

  • @Description: 随机生成字符串
  • @param n 字符串长度
  • @param mode 字符串模式 random.NUMBER|random.LOWERCASE|random.SPECIAL|random.CAPITAL)
  • @return str 生成的字符串

func RandStringSlice added in v0.11.1

func RandStringSlice(count, len int, mode RandType) (result []string)

RandStringSlice 指定长度的随机字符串

Types

type RandType

type RandType int
const (
	// CAPITAL 包含大写字母
	CAPITAL RandType = iota + 1 // 自定义扩展
	// LOWERCASE 包含小写字母
	LOWERCASE
	// SPECIAL 包含特殊字符
	SPECIAL
	// NUMBER 包含数字
	NUMBER
)

Jump to

Keyboard shortcuts

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