random

package
v6.4.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 4 more Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxInt  = 1<<(UintSize-1) - 1 // 1<<31 - 1 or 1<<63 - 1
	MinInt  = -MaxInt - 1         // -1 << 31 or -1 << 63
	MaxUint = 1<<UintSize - 1     // 1<<32 - 1 or 1<<64 - 1
)

Define maximum int sizes (correct on 32 bit or 64 bit systems)

View Source
const StringAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"

StringAlphabet is the valid characters supported by RandomString() below

View Source
const UintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64

UintSize is either 32 or 64. Bitwise complement 0 to all 1's, bitshift 32. a) Will be 0 on 32 bit systems causing 0 & 1 to be zero, leaving 32. b) Will be 1 on 64 bit systems causing 1 & 1 to be 1, shifting 32 to 64

Variables

This section is empty.

Functions

func RandByte

func RandByte() byte

RandByte returns a random byte

func RandByteSlice

func RandByteSlice() []byte

RandByteSlice returns a random byte slice of length 0 <= len <= 63

func RandByteSliceOfLen

func RandByteSliceOfLen(l int) []byte

RandByteSliceOfLen returns a random byte slice of specified length

func RandInt

func RandInt() int

RandInt returns a random integer between [0,MaxInt64)

func RandInt64

func RandInt64() int64

RandInt64 returns a random number between [0,MaxInt64). Note - even though the return int64 is signed, the underlying function call should never return a negative value.

func RandInt64Between

func RandInt64Between(min, max int64) int64

RandInt64Between returns a random number between [min,max). If min>=max then returns 0. It takes special care to ensure the int64 difference between max-min doesn't overflow. Worst case int64 can support from -N to N-1 which if input into this system as max/min creates (N-1)-(-N) = 2*N-1 number which would overflow int64. To deal with this, we make max-min a uint64 and get a random uint64. If the random number is larger than int64 maximum value, then we first add the max int64 to the min value, and subtract the max int64 from the random uint64 number, thus breaking the addition into two smaller pieces which each fit within the int64 size

func RandIntBetween

func RandIntBetween(min, max int) int

RandIntBetween returns a random number between [min,max). If min>=max then returns 0. The same special care is used as explained in the above RandInt64Between function. Here we never allow something larger than MaxInt to be added to the int, which preserves safe addition if we are on a 32 bit or 64 bit system.

func RandNonEmptyByteSlice

func RandNonEmptyByteSlice() []byte

RandNonEmptyByteSlice returns a random byte slice of length 1 <= len <= 63 (guaranteed not zero length)

func RandUInt32

func RandUInt32() uint32

RandUInt32 returns a random number between [0,MaxUint32)

func RandUInt64

func RandUInt64() uint64

RandUInt64 returns a random number between [0,MaxUint64)

func RandUInt64Between

func RandUInt64Between(min, max uint64) uint64

RandUInt64Between returns a random number between [min,max). If min>=max then returns 0.

func RandUInt8

func RandUInt8() uint8

RandUInt8 returns a random number between [0,MaxUint8)

func RandomString

func RandomString() string

RandomString creates a random string of length 0 <= len < 128 consisting only of characters within the set of lower and uppercase letters, numbers 0-9, and the special characters associated with 'shift+<num>' on your keyboard

Types

This section is empty.

Jump to

Keyboard shortcuts

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