randx

package
v1.3.14 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 6 Imported by: 0

README

stringx

封装了随机数相关函数

Random with time.Now().UnixNano() seed.
  • RandInt [int.Min, int.Max] 包括负数
  • RandUint [uint.Min, uint.Max] 不包括负数
  • RandInt64 [int64.Min, int64.Max] 包括负数
  • RandUint64 [uint64.Min, uint64.Max] 不包括负数
  • RandInt32 [int32.Min, int32.Max] 包括负数
  • RandUint32 [uint32.Min, uint32.Max] 不包括负数

Random with system source(/dev/urandom), more security.

  • RandIntSecure [int.Min, int.Max] 包括负数
  • RandUintSecure [uint.Min, uint.Max] 不包括负数
  • RandInt64Secure [int64.Min, int64.Max] 包括负数
  • RandUint64Secure [uint64.Min, uint64.Max] 不包括负数
  • RandInt32Secure [int32.Min, int32.Max] 包括负数
  • RandUint32Secure [uint32.Min, uint32.Max] 不包括负数

Random in range.

Specific m and n(m <= n, otherwise will panic), random range in [m, n].

  • RandBetween use time.Now().UnixNano() as each random source.
  • SimpleRandBetween use global rand source, in each time call, time.Now().UnixNano() will feed in.
  • RandBetweenSecure feed system source(/dev/urandom) as each call source.

Shuffle

Shuffle slice.

  • Shuffle function to shuffle a slice.
  • SetShuffleRand set Random in range function.
    (Option1. RandBetween)
    (Option2. SimpleRandBetween)
    (Option3. RandBetweenSecure)

Documentation

Index

Constants

View Source
const (
	// ChanBufferSize defines channel buffer capacity: 1M * 8 bytes = 8MB total buffer
	ChanBufferSize = 1024 * 1024
	// ChanChunkSize defines how many bytes to read from crypto/rand each time: 1024 bytes
	ChanChunkSize = 1024
)

Variables

View Source
var (
	// ZeroUnit represents an all-zero 8-byte unit returned when generator is closed
	ZeroUnit = [8]byte{}
)

Functions

func Float64RangeSecure added in v1.3.11

func Float64RangeSecure(_min, _max float64) float64

Float64RangeSecure returns a random float64 in [min, max) range Uses linear transformation to maintain uniform distribution

func Float64Secure added in v1.3.11

func Float64Secure() float64

Float64Secure returns a float64 in [0, 1) range Uses 53 bits for proper float64 precision

func IntBetweenSecure added in v1.3.11

func IntBetweenSecure(_min, _max int) int

IntBetweenSecure returns a random int in [min, max] range (inclusive) Convenient wrapper for common range operations

func IntNSecure added in v1.3.11

func IntNSecure(n int) int

IntNSecure random generate int, read data from linux /dev/urandom in range [0, n)

func IntRangeSecure added in v1.3.11

func IntRangeSecure(_min, _max int) int

IntRangeSecure returns a random int in [min, max) range Convenient wrapper for common range operations

func RandBetween

func RandBetween(minV, maxV int) int

RandBetween : generate rand between [a, b], return int

func RandBetweenSecure

func RandBetweenSecure(minV, maxV int) int

RandBetweenSecure : generate rand between [a, b], return int

func RandInt

func RandInt() int

RandInt random generate int, rand seed is current nano timestamp

func RandInt32

func RandInt32() int32

RandInt32 random generate int32, rand seed is current nano timestamp

func RandInt32Secure

func RandInt32Secure() int32

RandInt32Secure random generate int32, read data from linux /dev/urandom actually, it's almost a random id

func RandInt64

func RandInt64() int64

RandInt64 random generate int64, rand seed is current nano timestamp

func RandInt64Secure

func RandInt64Secure() int64

RandInt64Secure random generate int64, read data from linux /dev/urandom actually, it's almost a random id

func RandIntSecure

func RandIntSecure() int

RandIntSecure random generate int, read data from linux /dev/urandom actually, it's almost a random id

func RandUint

func RandUint() uint

RandUint random generate uint, rand seed is current nano timestamp

func RandUint32

func RandUint32() uint32

RandUint32 random generate uint32, rand seed is current nano timestamp

func RandUint32Secure

func RandUint32Secure() uint32

RandUint32Secure random generate uint32, read data from linux /dev/urandom actually, it's almost a random id

func RandUint64

func RandUint64() uint64

RandUint64 random generate uint64, rand seed is current nano timestamp

func RandUint64Secure

func RandUint64Secure() uint64

RandUint64Secure random generate uint64, read data from linux /dev/urandom actually, it's almost a random id

func RandUintSecure

func RandUintSecure() uint

RandUintSecure random generate uint, read data from linux /dev/urandom actually, it's almost a random id

func Shuffle

func Shuffle(ss Swapper)

Shuffle : shuffle slice

func SimpleRandBetween

func SimpleRandBetween(minV, maxV int) int

SimpleRandBetween : generate rand between [a, b], use same global rand source: rand.Rand

Types

type PassGen added in v1.3.12

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

PassGen is a password generator

func NewPassGen added in v1.3.12

func NewPassGen(specialChars []byte) *PassGen

NewPassGen creates a new PassGen instance

func (*PassGen) GenerateRandomPassword added in v1.3.12

func (x *PassGen) GenerateRandomPassword(length int) string

GenerateRandomPassword generates a random password of specified length Requirements: - length must be at least 4 - must contain at least 1 lowercase letter - must contain at least 1 uppercase letter - must contain at least 1 digit - must contain at least 1 special character

type RandGen added in v1.3.12

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

RandGen is a random string generator

func NewRandGen added in v1.3.12

func NewRandGen(randChars []byte, length int) *RandGen

NewRandGen creates a new RandGen instance

func (*RandGen) Gen added in v1.3.12

func (rg *RandGen) Gen() string

Gen : generates a random string

type RandomChan added in v1.2.9

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

RandomChan provides high-performance access to crypto/rand through channel buffering Uses 8-byte units for optimal performance with common operations like Uint64() and Float64()

func NewRandomChan added in v1.2.9

func NewRandomChan() *RandomChan

NewRandomChan creates a new channel-based crypto random generator The generator uses a background goroutine to continuously fill the channel with random data

func (*RandomChan) Close added in v1.2.9

func (x *RandomChan) Close()

Close stops the random generator gracefully Can be called multiple times safely due to sync.Once

func (*RandomChan) Float64 added in v1.2.9

func (x *RandomChan) Float64() float64

Float64 returns a float64 in [0, 1) range Uses 53 bits for proper float64 precision

func (*RandomChan) Float64Range added in v1.2.9

func (x *RandomChan) Float64Range(_min, _max float64) float64

Float64Range returns a random float64 in [min, max) range Uses linear transformation to maintain uniform distribution

func (*RandomChan) IntBetween added in v1.2.9

func (x *RandomChan) IntBetween(_min, _max int) int

IntBetween returns a random int in [min, max] range (inclusive) Convenient wrapper for common range operations

func (*RandomChan) IntN added in v1.2.9

func (x *RandomChan) IntN(n int) int

IntN returns a random int in [0, n) range Uses rejection sampling to ensure uniform distribution

func (*RandomChan) IntRange added in v1.2.9

func (x *RandomChan) IntRange(_min, _max int) int

IntRange returns a random int in [min, max) range Convenient wrapper for common range operations

func (*RandomChan) ReadBytes added in v1.2.9

func (x *RandomChan) ReadBytes(n int) []byte

ReadBytes reads n bytes from the generator Note: May waste some bytes for non-8-aligned sizes, but this is acceptable since most use cases need 8-byte multiples (Uint64, Float64, etc.)

func (*RandomChan) Uint64 added in v1.2.9

func (x *RandomChan) Uint64() uint64

Uint64 reads a uint64 random number Most efficient method as it uses exactly one 8-byte unit

type Swapper

type Swapper interface {
	Swap(i, j int)
	Len() int
}

Jump to

Keyboard shortcuts

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