Skip to content
本页内容

# Random ​

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

## 源码: ​

- https://github.com/duke-git/lancet/blob/main/random/random.go

## 用法: ​

go

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

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

## 目录 ​

- RandBytes
- RandInt
- RandString
- RandUpper
- RandLower
- RandNumeral
- RandNumeralOrLetter
- UUIdV4
- RandUniqueIntSlice

## 文档 ​

### RandBytes ​

生成随机字节切片

函数签名:

go

    func RandBytes(length int) []byte

    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)
    }

    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

    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)
    }

    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

    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
    }

    package main

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

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

### RandUpper ​

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

函数签名:

go

    func RandUpper(length int) string

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    package main

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

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

### UUIdV4 ​

生成UUID v4字符串

函数签名:

go

    func UUIdV4() (string, error)

    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)
    }

    package main

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

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

### RandUniqueIntSlice ​

生成一个不重复的长度为n的随机int切片。

函数签名:

go

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

    func RandUniqueIntSlice(n, 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)
    }

    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)
    }