Documentation ¶
Index ¶
- Constants
- func RandByte() byte
- func RandByteSlice() []byte
- func RandByteSliceOfLen(l int) []byte
- func RandInt() int
- func RandInt64() int64
- func RandInt64Between(min, max int64) int64
- func RandIntBetween(min, max int) int
- func RandNonEmptyByteSlice() []byte
- func RandUInt32() uint32
- func RandUInt64() uint64
- func RandUInt64Between(min, max uint64) uint64
- func RandUInt8() uint8
- func RandomString() string
Constants ¶
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)
const StringAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
StringAlphabet is the valid characters supported by RandomString() below
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 RandByteSlice ¶
func RandByteSlice() []byte
RandByteSlice returns a random byte slice of length 0 <= len <= 63
func RandByteSliceOfLen ¶
RandByteSliceOfLen returns a random byte slice of specified length
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 ¶
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 ¶
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 RandUInt64Between ¶
RandUInt64Between returns a random number between [min,max). If min>=max then returns 0.
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.