Skip to content

Random

random 随机数生成器包,可以生成随机[]bytes, int, string。

源码:

用法:

go
import (
    "github.com/duke-git/lancet/v2/random"
)

目录

文档

RandBytes

生成随机字节切片

函数签名:

go
func RandBytes(length int) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    randBytes := random.RandBytes(4)
    fmt.Println(randBytes)
}

RandInt

生成随机int, 范围[min, max)

函数签名:

go
func RandInt(min, max int) int

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    rInt := random.RandInt(1, 10)
    fmt.Println(rInt)
}

RandString

生成给定长度的随机字符串,只包含字母(a-zA-Z)

函数签名:

go
func RandString(length int) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    randStr := random.RandString(6)
    fmt.Println(randStr) //pGWsze
}

RandFromGivenSlice

从给定切片中随机生成元素。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    nicknames := []string{"张三", "李四", "王五", "赵六", "钱七"}
    randElm := random.RandFromGivenSlice(nicknames)
    fmt.Println(randElm)
}

RandSliceFromGivenSlice

从给定切片中生成长度为 num 的随机切片。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

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

RandUpper

生成给定长度的随机大写字母字符串

函数签名:

go
func RandUpper(length int) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    randStr := random.RandString(6)
    fmt.Println(randStr) //PACWGF
}

RandLower

生成给定长度的随机小写字母字符串

函数签名:

go
func RandLower(length int) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    randStr := random.RandLower(6)
    fmt.Println(randStr) //siqbew
}

RandNumeral

生成给定长度的随机数字字符串

函数签名:

go
func RandNumeral(length int) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    randStr := random.RandNumeral(6)
    fmt.Println(randStr) //035172
}

RandNumeralOrLetter

生成给定长度的随机字符串(数字+字母)

函数签名:

go
func RandNumeralOrLetter(length int) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    randStr := random.RandNumeralOrLetter(6)
    fmt.Println(randStr) //0aW7cQ
}

RandSymbolChar

生成给定长度的随机符号字符串。

函数签名:

go
func RandSymbolChar(length int) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    randStr := random.RandSymbolChar(6)
    fmt.Println(randStr) // 随机特殊字符字符串,例如: @#(_")
}

UUIdV4

生成UUID v4字符串

函数签名:

go
func UUIdV4() (string, error)

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    uuid, err := random.UUIdV4()
    if err != nil {
        return
    }
    fmt.Println(uuid)
}

RandIntSlice

生成一个特定长度的随机int切片,数值范围[min, max)。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    result := random.RandIntSlice(5, 0, 10)
    fmt.Println(result) //[1 2 7 1 5] (random)
}

RandUniqueIntSlice

生成一个特定长度的,数值不重复的随机int切片,数值范围[min, max)。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    result := random.RandUniqueIntSlice(5, 0, 10)
    fmt.Println(result) //[0 4 7 1 5] (random)
}

RandFloat

生成一个随机float64数值,可以指定精度。数值范围[min, max)。

函数签名:

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

实例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    floatNumber := random.RandFloat(1.0, 5.0, 2)
    fmt.Println(floatNumber) //2.14 (random number)
}

RandFloats

生成一个特定长度的随机float64切片,可以指定数值精度。数值范围[min, max)。

函数签名:

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

实例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    floatNumbers := random.RandFloats(5, 1.0, 5.0, 2)
    fmt.Println(floatNumber) //[3.42 3.99 1.3 2.38 4.23] (random)
}

RandStringSlice

生成随机字符串slice. 字符串类型需要是以下几种或者它们的组合: random.Numeral, random.LowwerLetters, random.UpperLetters random.Letters, random.SymbolChars, random.AllChars。

函数签名:

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

实例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    strs := random.RandStringSlice(random.Letters, 4, 6)
    fmt.Println(strs)

    // output random string slice like below:
    //[CooSMq RUFjDz FAeMPf heRyGv]
}

RandBool

生成随机bool值(true or false)。

函数签名:

go
func RandBool() bool

实例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    result := random.RandBool()
    fmt.Println(result) // true or false (random)
}

RandBoolSlice

生成特定长度的随机bool slice。

函数签名:

go
func RandBoolSlice(length int) []bool

实例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    result := random.RandBoolSlice(2)
    fmt.Println(result) // [true false] (random)
}

RandNumberOfLength

生成指定长度的随机数。

函数签名:

go
func RandNumberOfLength(len int) int

实例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
    i := random.RandNumberOfLength(2)
    fmt.Println(i) // 21 (random number of length 2)
}