krand

package
v1.10.3 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 3 Imported by: 4

README

krand

Generate random strings, integers, floating point numbers.


Example of use

Generate a random string

    import "github.com/zhufuyi/sponge/pkg/krand"

    /*
	R_NUM = 1      // only number
	R_UPPER = 2   // only capital letters
	R_LOWER = 4  // only lowercase letters
	R_All = 7	       // numbers, upper and lower case letters
    */

	// by | or combining different types
    kind := krand.R_NUM|krand.R_UPPER    // capital letters and numbers

	// a random string of length 10, consisting of upper case letters and numbers
    krand.String(kind, 10)

Generate random integers

    import "github.com/zhufuyi/sponge/pkg/krand"

    krand.Int(200)            // random number range 0 ~ 200
    krand.Int(1000, 2000)  // random number range 1000 ~ 2000

Generate random floating point numbers

    import "github.com/zhufuyi/sponge/pkg/krand"

    krand.Float64(1, 200)            // floating point number with 1 decimal point, range 0~200
    krand.Float64(2, 100,1000)            // floating point number with 2 decimal places, range 100~1000

Generate id

    import "github.com/zhufuyi/sponge/pkg/krand"

    krand.NewID()  // generate a id, example: 1701234567890397409
    krand.NewStringID()  // generate a string id, example: 179bffd372b8e8e1

    krand.NewSeriesID()  // generate a string id, example: 20060102150405000000123456

Documentation

Overview

Package krand is a library for generating random strings, integers, floating point numbers.

Index

Constants

View Source
const (
	R_NUM   = 1 // only number
	R_UPPER = 2 // only capital letters
	R_LOWER = 4 // only lowercase letters
	R_All   = 7 // numbers, upper and lower case letters
)

nolint

Variables

This section is empty.

Functions

func Bytes

func Bytes(kind int, bytesLen ...int) []byte

Bytes generate random strings of any length of multiple types, default length is 6 if bytesLen is empty example: Bytes(R_ALL), Bytes(R_ALL, 16), Bytes(R_NUM|R_LOWER, 16)

func Float64

func Float64(dpLength int, rangeSize ...int) float64

Float64 generates a random floating point number of the specified range size, Four types of passing references are supported, example: Float64(dpLength), Float64(dpLength, max), Float64(dpLength, min, max), Float64(dpLength, max, min), min<=random numbers<=max

func Int

func Int(rangeSize ...int) int

Int generate random numbers of specified range size, compatible with Int(), Int(max), Int(min, max), Int(max, min) 4 ways, min<=random number<=max

func NewID added in v1.10.0

func NewID() int64

NewID Generate a milliseconds+random number ID.

func NewSeriesID added in v1.10.0

func NewSeriesID() string

NewSeriesID Generate a datetime+random string ID, datetime is microsecond precision, 20 bytes, random is 6 bytes, total 26 bytes. example: 20060102150405000000123456

func NewStringID added in v1.10.0

func NewStringID() string

NewStringID Generate a string ID, the hexadecimal form of NewID(), total 16 bytes.

func String

func String(kind int, size ...int) string

String generate random strings of any length of multiple types, default length is 6 if size is empty example: String(R_ALL), String(R_ALL, 16), String(R_NUM|R_LOWER, 16)

Types

This section is empty.

Jump to

Keyboard shortcuts

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