randkit

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetStringBuilder

func GetStringBuilder() *strings.Builder

func PickFromList

func PickFromList[T any](rng *rand.Rand, list []T) T

PickFromList selects a random element from the provided list using the given random number generator. If the list is empty, it returns the zero value for the type T.

func PickFromMapKeys

func PickFromMapKeys[K comparable, V any](rng *rand.Rand, m map[K]V) K

PickFromMapKeys selects a random key from the provided map using the given random number generator. If the map is empty, it returns the zero value for the key type K.

func PickFromMapValues

func PickFromMapValues[K comparable, V any](rng *rand.Rand, m map[K]V) V

PickFromMapValues selects a random value from the provided map using the given random number generator. If the map is empty, it returns the zero value for the value type V.

func PutStringBuilder

func PutStringBuilder(sb *strings.Builder)

func RandomASCIICharacter

func RandomASCIICharacter(rng *rand.Rand) string

RandomASCIICharacter generates a random printable ASCII character (from 32 to 126) using the provided random number generator.

func RandomAlphanumeric

func RandomAlphanumeric(rng *rand.Rand, upperCase bool) string

RandomAlphanumeric generates a random alphanumeric character (0-9, a-z, A-Z) based on the upperCase parameter using the provided random number generator.

func RandomBool

func RandomBool(rng *rand.Rand, probTrue float64) bool

RandomBool generates a random boolean value based on the provided probability of being true using the given random number generator. probTrue should be a value between 0.0 and 1.0, where 0.0 means always false and 1.0 means always true.

func RandomDateTime

func RandomDateTime(rng *rand.Rand, from time.Time, to time.Time) time.Time

RandomDateTime generates a random time between the provided 'from' and 'to' times using the provided random number generator.

func RandomDigit

func RandomDigit(rng *rand.Rand) int

RandomDigit generates a random digit (0-9) using the provided random number generator.

func RandomFloat

func RandomFloat(rng *rand.Rand) float64

func RandomFloatBetween

func RandomFloatBetween(rng *rand.Rand, decimals int, decimalsExact bool, min, max float64) float64

RandomFloatBetween generates a random float between min and max (inclusive) with the specified number of decimal places using the provided random number generator. If min is greater than max, they will be swapped to ensure a valid range. If max is 0, it will be treated as math.MaxFloat64. If decimalsExact is true, the result will be rounded to the specified 'decimals' decimal places. If false, the result may have up to 'decimals' decimal places.

func RandomInteger

func RandomInteger[T int | int32 | int64 | uint | uint32 | uint64](rng *rand.Rand) T

RandomInteger generates a random integer of the specified type using the provided random number generator.

func RandomIntegerBetween

func RandomIntegerBetween[T int | int32 | int64 | uint | uint32 | uint64](rng *rand.Rand, min, max T) T

RandomIntegerBetween generates a random integer between min and max (inclusive) using the provided random number generator. If min is greater than max, they will be swapped to ensure a valid range. If max is 0, it will be treated as the maximum value for the type T (e.g., math.MaxInt for int).

func RandomLetter

func RandomLetter(rng *rand.Rand, upperCase bool) string

RandomLetter generates a random letter (A-Z or a-z) based on the upperCase parameter using the provided random number generator.

func RandomSeed

func RandomSeed() uint64

RandomSeed creates a unique seed by combining the current time, process ID, and an atomic counter, to minimize the chances of collision.

func RandomString

func RandomString(rng *rand.Rand, length int, upperCase bool) string

RandomString generates a random string of the specified length and case using the provided random number generator. If upperCase is true, the string will contain only uppercase letters. The length is capped at 'stringLength' to prevent excessive memory usage.

func RandomStringTemplate

func RandomStringTemplate(rng *rand.Rand, template string) string

RandomStringTemplate generates a random string filling in the provided template, where: - '\d' is replaced with a random digit (0-9) - '\a' is replaced with a random alphanumeric character (0-9, a-z) - '\A' is replaced with a random alphanumeric character (0-9, A-Z) - '\l' is replaced with a random lowercase letter (a-z) - '\L' is replaced with a random uppercase letter (A-Z) - '\.' is replaced with a random printable ASCII 32-126 character Any other characters in the template are left unchanged.

func ShuffleString

func ShuffleString(rng *rand.Rand, s string) string

ShuffleString takes a string and returns a new string with the characters shuffled in random order using the provided random number generator.

Types

This section is empty.

Jump to

Keyboard shortcuts

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