randomjez

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package randomjez 随机数生成相关函数

Index

Examples

Constants

View Source
const (
	Numeral          = "0123456789"
	LowerCaseLetters = "abcdefghijklmnopqrstuvwxyz"
	UpperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

Variables

This section is empty.

Functions

func Random

func Random(s string, n int, unique ...bool) string

Random 随机生成字符串

参数:

  • s: 字符集合,用于生成随机字符串
  • n: 生成的随机字符串的长度
  • unique: 可选参数,指定是否生成不重复的字符串,默认为 false

返回值:

  • 生成的随机字符串

注意事项:

  • 当 n < 1 时,会 panic
  • 即使 unique 为 true 时,但如果传入的字符串有重复元素,那么生成的字符串可能也会重复
  • 当 unique 为 true 时,如果 n 大于 s 的长度,那么会 panic
Example
res := Random(Numeral, 11)

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

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { Random(Numeral, 0) })
isPanic(func() { Random(Numeral, 11, true) })
Output:

true
11
true
true

func RandomBytes

func RandomBytes(n int) []byte

RandomBytes 随机生成字节切片

注意事项:

  • 当 n < 1 时,会 panic
Example
res := RandomBytes(10)

fmt.Println(len(res))
isPanic(func() { RandomBytes(0) })
Output:

10
true

func RandomCaseLetters

func RandomCaseLetters(n int, unique ...bool) string

RandomCaseLetters 随机生成大小写字母字符串

注意事项:

  • 当 n < 1 时,会 panic
  • 当 unique 为 true 时,如果 n 大于 52(26个大写字母+26个小写字母),那么会 panic
Example
res := RandomCaseLetters(53)

reg := regexp.MustCompile(`^[A-Za-z]+$`)

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { RandomCaseLetters(0) })
isPanic(func() { RandomCaseLetters(53, true) })
Output:

true
53
true
true

func RandomCharset

func RandomCharset(n int, unique ...bool) string

RandomCharset 随机生成字符串,包含数字、大小写字母

注意事项:

  • 当 n < 1 时,会 panic
  • 当 unique 为 true 时,如果 n 大于 62(10个数字+26个大写字母+26个小写字母),那么会 panic
Example
res := RandomCharset(63)

reg := regexp.MustCompile(`^[A-Za-z0-9]+$`)

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { RandomUpperNumeral(0) })
isPanic(func() { RandomUpperNumeral(63, true) })
Output:

true
63
true
true

func RandomInt

func RandomInt(min, max int) int

RandomInt 随机生成整数,包含 min,不包含 max,即 [min,max)

注意事项:

  • 当 min > max 时,会交换 min 和 max 的值
Example
min := 10
max := 15

res := RandomInt(min, max)

if res < min || res >= max {
	fmt.Println(false)
} else {
	fmt.Println(true)
}
Output:

true

func RandomIntSlice

func RandomIntSlice(min, max, n int) []int

RandomIntSlice 随机生成整数切片

注意事项:

  • 当 min > max 时,会交换 min 和 max 的值
  • 当 n < 1 时,会 panic
Example
min := 10
max := 15

res := RandomIntSlice(min, max, 10)

ok := true

for _, r := range res {
	if r < min || r >= max {
		ok = false
		break
	}
}

fmt.Println(ok)
fmt.Println(len(res))
isPanic(func() { RandomIntSlice(min, max, 0) })
Output:

true
10
true

func RandomIntSliceUnique

func RandomIntSliceUnique(min, max, n int) []int

RandomIntSliceUnique 随机生成不重复的整数切片

注意事项:

  • 当 min > max 时,会交换 min 和 max 的值
  • 当 n < 1 时,会 panic
  • 当 n 大于 max-min+1 时,会 panic
Example
min := 10
max := 15

res := RandomIntSliceUnique(min, max, 5)

var (
	ok    = true
	exist = make(map[int]struct{}, 5)
)

for _, r := range res {
	if r < min || r >= max {
		ok = false
		break
	}
	if _, ok1 := exist[r]; ok1 {
		ok = false
		break
	}
	exist[r] = struct{}{}
}

fmt.Println(ok)
fmt.Println(len(res))

isPanic(func() { RandomIntSliceUnique(min, max, 0) })
isPanic(func() { RandomIntSliceUnique(min, max, 10) })
Output:

true
5
true
true

func RandomLower

func RandomLower(n int, unique ...bool) string

RandomLower 随机生成小写字母字符串

注意事项:

  • 当 n < 1 时,会 panic
  • 当 unique 为 true 时,如果 n 大于 26(26个字母),那么会 panic
Example
res := RandomLower(27)

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

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { RandomLower(0) })
isPanic(func() { RandomLower(27, true) })
Output:

true
27
true
true

func RandomLowerNumeral

func RandomLowerNumeral(n int, unique ...bool) string

RandomLowerNumeral 随机生成小写字母和数字字符串

注意事项:

  • 当 n < 1 时,会 panic
  • 当 unique 为 true 时,如果 n 大于 36(10个数字+26个字母),那么会 panic
Example
res := RandomLowerNumeral(37)

reg := regexp.MustCompile(`^[a-z0-9]+$`)

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { RandomLowerNumeral(0) })
isPanic(func() { RandomLowerNumeral(37, true) })
Output:

true
37
true
true

func RandomNumeral

func RandomNumeral(n int, unique ...bool) string

RandomNumeral 随机生成数字字符串

注意事项:

  • 当 n < 1 时,会 panic
  • 当 unique 为 true 时,如果 n 大于 10(10个数字),那么会 panic
Example
res := RandomNumeral(11)

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

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { RandomNumeral(0) })
isPanic(func() { RandomNumeral(11, true) })
Output:

true
11
true
true

func RandomUpper

func RandomUpper(n int, unique ...bool) string

RandomUpper 随机生成大写字母字符串

注意事项:

  • 当 n < 1 时,会 panic
  • 当 unique 为 true 时,如果 n 大于 26(26个字母),那么会 panic
Example
res := RandomUpper(27)

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

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { RandomUpper(0) })
isPanic(func() { RandomUpper(27, true) })
Output:

true
27
true
true

func RandomUpperNumeral

func RandomUpperNumeral(n int, unique ...bool) string

RandomUpperNumeral 随机生成大写字母和数字字符串

注意事项:

  • 当 n < 1 时,会 panic
  • 当 unique 为 true 时,如果 n 大于 36(10个数字+26个字母),那么会 panic
Example
res := RandomUpperNumeral(37)

reg := regexp.MustCompile(`^[A-Z0-9]+$`)

fmt.Println(reg.MatchString(res))
fmt.Println(len(res))
isPanic(func() { RandomUpperNumeral(0) })
isPanic(func() { RandomUpperNumeral(37, true) })
Output:

true
37
true
true

func Sample

func Sample[T any](list []T) T

Sample 从切片中随机返回一个元素。

Example
list := []int{1, 2, 3, 3, 5, 3, 5, 6}
var list2 []string

s := Sample(list)

ok := false
for _, v := range list {
	if v == s {
		ok = true
		break
	}
}

fmt.Println(ok)
fmt.Println(Sample(list2) == "")
Output:

true
true

func Samples

func Samples[T any](list []T, n int) []T

Samples 从切片中随机返回n个元素,结果不去重。

注意事项:

  • 如果 n 大于切片的长度,则返回打乱顺序后的切片,如果 n 小于等于0,则返回空切片。
Example
list := []int{1, 2, 3}

s2 := Samples(list, 3)

ok := true
for _, v := range s2 {
	if v > 3 || v < 1 {
		ok = false
		break
	}
}

fmt.Println(ok)

fmt.Println(Samples([]string{}, 3))
Output:

true
[]

func Shuffle

func Shuffle[T any](list []T) []T

Shuffle 打乱切片中元素的顺序。

Example
list := []int{1, 2, 3, 3, 5, 3, 5, 6}

list2 := Shuffle([]int{})

fmt.Println(len(list) == len(Shuffle(list)))
fmt.Println(list2)
Output:

true
[]

func UUIDv4

func UUIDv4() string

UUIDv4 根据 RFC4122 生成 UUID v4版本

注意事项:

  • 这种方法生成的 UUID v4 可能不是完全符合标准,但在大多数情况下应该是足够的。
Example
uuid := UUIDv4()

reg := regexp.MustCompile(`^[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}$`)

fmt.Println(len(uuid))
fmt.Println(reg.MatchString(uuid))
Output:

36
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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