rand

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2015 License: MIT Imports: 3 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ALPHABET     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	NUMERALS     = "0123456789"
	ALPHANUMERIC = NUMERALS + ALPHABET

	ErrNegativeNum  = errors.Err("Number cannot be negative")
	ErrEmptyCharset = errors.Err("Charset cannot be empty")
)

Charset of characters to use for generating random strings

Variables

This section is empty.

Functions

This section is empty.

Types

type BytesFunc

type BytesFunc func(n int, charset string) ([]byte, error)
var B BytesFunc = func(n int, charset string) ([]byte, error) {
	if n <= 0 || len(charset) == 0 {
		panic("negative number or empty charset")
	}

	result := make([]byte, n)
	charnum := big.NewInt(int64(len(charset)))
	for i := 0; i < n; i++ {
		r, err := rand.Int(rand.Reader, charnum)
		if err != nil {
			return nil, err
		}

		result[i] = charset[int(r.Int64())]
	}
	return result, nil
}

B generate a random bytes in gived charset

func (BytesFunc) Alphabet

func (f BytesFunc) Alphabet(n int) ([]byte, error)

AlphabetBytes generate random ALPHABET bytes

func (BytesFunc) Alphanumeric

func (f BytesFunc) Alphanumeric(n int) ([]byte, error)

AlphanumericBytes generate random ALPHABET and numberic bytes

func (BytesFunc) Numberal

func (f BytesFunc) Numberal(n int) ([]byte, error)

NumberalBytes generate random ASCII bytes

type StringFunc

type StringFunc func(n int, charset string) (string, error)
var S StringFunc = func(n int, charset string) (string, error) {
	b, err := B(n, charset)
	if err != nil {
		return "", err
	}

	return string(b), nil
}

String is same as Bytes, return string

func (StringFunc) Alphabet

func (f StringFunc) Alphabet(n int) (string, error)

Alphabet generate random ALPHABET string

func (StringFunc) Alphanumeric

func (f StringFunc) Alphanumeric(n int) (string, error)

Alphanumeric generate random ALPHABET and numberic string

func (StringFunc) Numberal

func (f StringFunc) Numberal(n int) (string, error)

Numberal generate random numberal string

Jump to

Keyboard shortcuts

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