random

package
v0.0.0-...-24ca9bf Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

There are several broad categories of random data that can be generated by random:

Text

Text data are largely represented by the string (utf8 compliant by design in Go) and []byte types. They are somewhat interchangeable and have similar standard library methods available. The 'text' type is a generic Go (requires v.1.18+) type interface:

type text interface {
	~string | ~[]byte
}

It is used internally to create generic functions that may accept either string types or []byte as input. The type is not exported because of several odd behaviors that prevent a 'perfect' generic relationship between the underlying types.

Integers

There are generic functions for the generation of random integers of various flavors, as well as sets and sequences of such integers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigInt

func BigInt(max *big.Int) (n *big.Int, err error)

BigInt returns a uniform random Int in [0, max). An Int from the big package represents a signed multi-precision integer. The Int returned is safe for concurrent and cryptographically secure applications.

For convenience, if max == 0 or max == nil, a new big.Int with the zero value (0) is returned. If max is < 0, the absolute value of max is used instead. (This may lead to unexpected errors. It is the caller's responsibility to check the intended functionality for max <= 0 and provide a guard if necessary.)

Operations always take pointer arguments (*Int) rather than Int values, and each unique Int value requires its own unique *Int pointer. To "copy" an Int value, an existing (or newly allocated) Int must be set to a new value using the Int.Set method; shallow copies of Ints are not supported and may lead to errors.

func CreateRandomLengthText

func CreateRandomLengthText[T text](min, max int) (retval T, err error)

RandText returns a random string or slice of random bytes with a length between min and max.

func CreateRandomTextSets

func CreateRandomTextSets[T text](n, min, max int) ([]T, error)

func CreateTextSets

func CreateTextSets[T text](n, mult int) ([]T, error)

CreateTextSets creates n sets of random strings or byte slices that are 1<<(n*mult) in length

func Int

func Int[In, Out Ints](max In) Out

Int returns a uniform random value in [0, max) of the type specified by the constraint T. The value returned is safe for concurrent and cryptographically secure applications.

For convenience, if max == 0 or max == nil, 0 is returned. If max is < 0, the absolute value of max is used instead. (This may lead to unexpected errors. It is the caller's responsibility to check the intended functionality for max <= 0 and provide a guard if necessary.)

func NewBigInt

func NewBigInt(x int64) *big.Int

NewBigInt returns a new big.Int pointer with a value equal to the given int64 parameter.

func RandByte

func RandByte() (byte, error)

RandByte returns a random byte. It uses the default crypto/rand package source and is safe for concurrent use by multiple goroutines.

func RandText

func RandText[T text](n int) (T, error)

RandText returns a random string or slice of random bytes n bytes long. It uses the default crypto/rand package source and is safe for concurrent use by multiple goroutines.

func RandomReader

func RandomReader() io.Reader

RandomReader returns an alias to a global, shared instance of a concurrency safe, cryptographically secure random number generator directly from the Go standard library crypto/rand package.

On Linux, FreeBSD, Dragonfly and Solaris, Reader uses getrandom(2) if available, /dev/urandom otherwise. On OpenBSD and macOS, Reader uses getentropy(2). On other Unix-like systems, Reader reads from /dev/urandom. On Windows systems, Reader uses the RtlGenRandom API. On Wasm, Reader uses the Web Crypto API.

func Read

func Read(b []byte) (n int, err error)

Read is a helper function that reads exactly len(b) random bytes into b. It uses a concurrency safe, cryptographically secure random number generator directly from the Go standard library crypto/rand package.

It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading fewer than min bytes, ReadAtLeast returns ErrUnexpectedEOF. If len(b) is greater than the length of b, Read returns ErrShortBuffer.

On return, n == len(b) if and only if err == nil. If r returns an error having read at least len(buf) bytes, the error is dropped. Reference: standard library io.Read

func Rnd

func Rnd() (int, error)

Types

type Ints

type Ints constraints.Integer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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