rand

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Numbers = 1 << iota
	Letters = 1 << iota
	Upper   = 1 << iota
	Lower   = 1 << iota
)

options to generate random string

Variables

This section is empty.

Functions

func CheckOption

func CheckOption(option, length, numbers int) error

CheckOption checks if the given option is valid.

func Int63

func Int63() int64

Int63 returns a non-negative pseudo-random 63-bit integer as an int64.

func String

func String(length int) string

String same as StringWithOption(length, Numbers | Letters), use a separate function with constants to improve performance.

func StringWithOption

func StringWithOption(randomLength, option int, prefix, suffix string) string

StringWithOption returns a random string with the given length and options. The options can be combined using bitwise OR operation. The basic idea of this function is to generate a random number and map it to a character in the given options. This algorithm and idea is from https://stackoverflow.com/a/31832326 with an optimization for thead-safe by pre-allocating a bulk of mutexes and rand.Source. NOTE: this function will not check the options, length, so you should make sure the options and length are valid and correct before calling this function. can use CheckOption to check the option, length and number.

func Uint64

func Uint64() uint64

Uint64 returns a random uint64 value.

Types

type Selector added in v0.1.6

type Selector interface {
	// Next returns true with the configured probability
	Next() bool
}

Selector is a probabilistic selector It provides a way to randomly select an item with a given probability It uses a thread-local random number generator to avoid contention and improve performance in concurrent scenarios

func NewSelector added in v0.1.6

func NewSelector(percentage float64) Selector

NewSelector creates a new Selector with the given percentage

Jump to

Keyboard shortcuts

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