Skip to content
On this page

# Random ​

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

## Source: ​

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

## Usage: ​

go

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

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

## Index ​

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

## Documentation ​

### RandBytes ​

Generate random byte slice.

Signature:

go

    func RandBytes(length int) []byte

    func RandBytes(length int) []byte

Example:Run

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 ​

Generate random int between min and max, may contain min, not max.

Signature:

go

    func RandInt(min, max int) int

    func RandInt(min, max int) int

Example:Run

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 ​

Generate random given length string. only contains letter (a-zA-Z)

Signature:

go

    func RandString(length int) string

    func RandString(length int) string

Example:Run

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 ​

Generate a random upper case string

Signature:

go

    func RandUpper(length int) string

    func RandUpper(length int) string

Example:Run

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 ​

Generate a random lower case string

Signature:

go

    func RandLower(length int) string

    func RandLower(length int) string

Example:Run

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 ​

Generate a random numeral string

Signature:

go

    func RandNumeral(length int) string

    func RandNumeral(length int) string

Example:Run

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 ​

generate a random numeral or letter string

Signature:

go

    func RandNumeralOrLetter(length int) string

    func RandNumeralOrLetter(length int) string

Example:Run

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 ​

Generate a random UUID of version 4 according to RFC 4122.

Signature:

go

    func UUIdV4() (string, error)

    func UUIdV4() (string, error)

Example:Run

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 ​

Generate a slice of random int of length n that do not repeat.

Signature:

go

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

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

Example:Run

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