random

package
v2.3.4 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 9 Imported by: 35

Documentation

Overview

Package random implements some basic functions to generate random int and string.

Index

Examples

Constants

View Source
const (
	MaximumCapacity = math.MaxInt32>>1 + 1
	Numeral         = "0123456789"
	LowwerLetters   = "abcdefghijklmnopqrstuvwxyz"
	UpperLetters    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Letters         = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	SymbolChars     = "!@#$%^&*()_+-=[]{}|;':\",./<>?"
	AllChars        = Numeral + LowwerLetters + UpperLetters + SymbolChars
)

Variables

This section is empty.

Functions

func RandBool added in v2.3.3

func RandBool() bool

RandBool generates a random boolean value (true or false). Play: https://go.dev/play/p/to6BLc26wBv

func RandBoolSlice added in v2.3.3

func RandBoolSlice(length int) []bool

RandBoolSlice generates a random boolean slice of specified length. Play: https://go.dev/play/p/o-VSjPjnILI

func RandBytes

func RandBytes(length int) []byte

RandBytes generate random byte slice. Play: https://go.dev/play/p/EkiLESeXf8d

Example
bytes := RandBytes(4)

fmt.Println(len(bytes))
Output:

4

func RandFloat added in v2.2.8

func RandFloat(min, max float64, precision int) float64

RandFloat generate random float64 number between [min, max) with specific precision. Play: https://go.dev/play/p/zbD_tuobJtr

Example
pattern := `^[\d{1}.\d{2}]+$`
reg := regexp.MustCompile(pattern)

num := RandFloat(1.0, 5.0, 2)

// check num is a random float in [1.0, 5.0)
result1 := num >= 1.0 && num < 5.0
result2 := reg.MatchString(strconv.FormatFloat(num, 'f', -1, 64))

fmt.Println(result1)
fmt.Println(result2)
Output:

true
true

func RandFloats added in v2.2.8

func RandFloats(length int, min, max float64, precision int) []float64

RandFloats generate a slice of random float64 numbers of length that do not repeat. Play: https://go.dev/play/p/I3yndUQ-rhh

Example
isInRange := true
numbers := RandFloats(5, 1.0, 5.0, 2)
for _, n := range numbers {
	isInRange = (n >= 1.0 && n < 5.0)
}

fmt.Println(isInRange)
fmt.Println(len(numbers))
Output:

true
5

func RandFromGivenSlice added in v2.3.3

func RandFromGivenSlice[T any](slice []T) T

RandFromGivenSlice generate a random element from given slice. Play: https://go.dev/play/p/UrkWueF6yYo

Example
goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon",
	"mango", "nectarine", "orange"}

result := RandFromGivenSlice(goods)
fmt.Println(result)
Output:

func RandInt

func RandInt(min, max int) int

RandInt generate random int between [min, max). Play: https://go.dev/play/p/pXyyAAI5YxD

Example
result := RandInt(1, 10)

if result >= 1 && result < 10 {
	fmt.Println("ok")
}
Output:

ok

func RandIntSlice added in v2.3.3

func RandIntSlice(length, min, max int) []int

RandIntSlice generates a slice of random integers. The generated integers are between min and max (exclusive). Play: https://go.dev/play/p/GATTQ5xTEG8

func RandLower added in v2.1.9

func RandLower(length int) string

RandLower generate a random lower case string of specified length. Play: https://go.dev/play/p/XJtZ471cmtI

Example
pattern := `^[a-z]+$`
reg := regexp.MustCompile(pattern)

s := RandLower(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandNumberOfLength added in v2.3.4

func RandNumberOfLength(len int) int

RandNumberOfLength 生成一个长度为len的随机数 Play: todo

func RandNumeral added in v2.1.9

func RandNumeral(length int) string

RandNumeral generate a random numeral string of specified length. Play: https://go.dev/play/p/g4JWVpHsJcf

Example
pattern := `^[0-9]+$`
reg := regexp.MustCompile(pattern)

s := RandNumeral(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandNumeralOrLetter added in v2.1.9

func RandNumeralOrLetter(length int) string

RandNumeralOrLetter generate a random numeral or alpha string of specified length. Play: https://go.dev/play/p/19CEQvpx2jD

Example
pattern := `^[0-9a-zA-Z]+$`
reg := regexp.MustCompile(pattern)

s := RandNumeralOrLetter(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandSliceFromGivenSlice added in v2.3.3

func RandSliceFromGivenSlice[T any](slice []T, num int, repeatable bool) []T

RandSliceFromGivenSlice generate a random slice of length num from given slice.

  • If repeatable is true, the generated slice may contain duplicate elements.

Play: https://go.dev/play/p/68UikN9d6VT

Example
goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon",
	"mango", "nectarine", "orange"}
chosen3goods := RandSliceFromGivenSlice(goods, 3, false)
fmt.Println(chosen3goods)
Output:

func RandString

func RandString(length int) string

RandString generate random alphabeta string of specified length. Play: https://go.dev/play/p/W2xvRUXA7Mi

Example
pattern := `^[a-zA-Z]+$`
reg := regexp.MustCompile(pattern)

s := RandString(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandStringSlice added in v2.3.3

func RandStringSlice(charset string, sliceLen, strLen int) []string

RandString generate a slice of random string of length strLen based on charset. chartset should be one of the following: random.Numeral, random.LowwerLetters, random.UpperLetters random.Letters, random.SymbolChars, random.AllChars. or a combination of them. Play: https://go.dev/play/p/2_-PiDv3tGn

func RandSymbolChar added in v2.2.8

func RandSymbolChar(length int) string

RandSymbolChar generate a random symbol char of specified length. symbol chars: !@#$%^&*()_+-=[]{}|;':\",./<>?. Play: https://go.dev/play/p/Im6ZJxAykOm

Example
pattern := `^[\W|_]+$`
reg := regexp.MustCompile(pattern)

s := RandSymbolChar(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandUniqueIntSlice added in v2.2.2

func RandUniqueIntSlice(length, min, max int) []int

RandUniqueIntSlice generate a slice of random int of length that do not repeat. Play: https://go.dev/play/p/uBkRSOz73Ec

Example
result := RandUniqueIntSlice(5, 0, 10)

if len(result) == 5 {
	fmt.Println("ok")
}
Output:

ok

func RandUpper added in v2.1.9

func RandUpper(length int) string

RandUpper generate a random upper case string of specified length. Play: https://go.dev/play/p/29QfOh0DVuh

Example
pattern := `^[A-Z]+$`
reg := regexp.MustCompile(pattern)

s := RandUpper(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func UUIdV4

func UUIdV4() (string, error)

UUIdV4 generate a random UUID of version 4 according to RFC 4122. Play: https://go.dev/play/p/_Z9SFmr28ft

Example
pattern := `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`
reg := regexp.MustCompile(pattern)

s, _ := UUIdV4()

result := reg.MatchString(s)

fmt.Println(result)
Output:

true

Types

This section is empty.

Jump to

Keyboard shortcuts

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