Skip to content

Random

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

Source:

Usage:

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

Index

Documentation

RandBytes

Generate random byte slice.

Signature:

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

RandInt

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

Signature:

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

RandString

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

Signature:

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

RandFromGivenSlice

Generate a random element from given slice.

Signature:

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

Example:Run

go
package main

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

func main() {
    randomSet := []any{"a", 8, "hello", true, 1.1}
    randElm := random.RandFromGivenSlice(randomSet)
    fmt.Println(randElm)
}

RandSliceFromGivenSlice

Generate a random slice of length num from given slice.

Signature:

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

Example:Run

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

Generate a random upper case string

Signature:

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

RandLower

Generate a random lower case string

Signature:

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

RandNumeral

Generate a random numeral string

Signature:

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

RandNumeralOrLetter

generate a random numeral or letter string

Signature:

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

RandSymbolChar

Generate a random symbol char of specified length.

Signature:

go
func RandSymbolChar(length int) string

Example:Run

go
package main

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

func main() {
    randStr := random.RandSymbolChar(6)
    fmt.Println(randStr) // random string like: @#(_")
}

UUIdV4

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

Signature:

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

RandIntSlice

Generate a slice of random int. Number range in [min, max)

Signature:

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

Example:Run

go
package main

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

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

RandUniqueIntSlice

Generate a slice of random int of length that do not repeat. Number range in [min, max)

Signature:

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

RandFloat

Generate a random float64 number between [min, max) with specific precision.

Signature:

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

Example:Run

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

Generate a slice of random float64 numbers of length n that do not repeat. Number range in [min, max)

Signature:

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

Example:Run

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(floatNumbers) //[3.42 3.99 1.3 2.38 4.23] (random)
}

RandStringSlice

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.

Signature:

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

Example:Run

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

Generate a random boolean value (true or false).

Signature:

go
func RandBool() bool

Example:Run

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

Generates a random boolean slice of specified length.

Signature:

go
func RandBoolSlice(length int) []bool

Example:Run

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

Generates a random int number of specified length.

Signature:

go
func RandNumberOfLength(len int) int

Signature:Run

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