randutil

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const PrintableKeyAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

PrintableKeyAlphabet to use with random string generation to produce strings that doesn't need to be escaped when found as a part of a key and is generally human printable.

Variables

This section is empty.

Functions

func FastInt63

func FastInt63() int64

FastInt63 returns a non-negative pseudo-random 63-bit integer as an int64. Compared to rand.Int63(), this implementation scales.

func FastUint32

func FastUint32() uint32

FastUint32 returns a lock free uint32 value. Compared to rand.Uint32, this implementation scales. We're using the go runtime's implementation through a linker trick.

func NewLockedPseudoRand

func NewLockedPseudoRand() (*rand.Rand, int64)

Same as NewPseudoRand, but the returned Rand is using thread safe underlying source.

func NewLockedSource

func NewLockedSource(seed int64) rand.Source

NewLockedSource creates random source protected by mutex.

func NewLockedTestRand

func NewLockedTestRand() (*rand.Rand, int64)

NewLockedTestRand is identical to NewTestRand but returned rand.Rand is using thread safe underlying source.

func NewPseudoRand

func NewPseudoRand() (*rand.Rand, int64)

NewPseudoRand returns an instance of math/rand.Rand seeded from the environment variable COCKROACH_RANDOM_SEED. If that variable is not set, crypto/rand is used to generate a seed. The seed is also returned so we can easily and cheaply generate unique streams of numbers. The created object is not safe for concurrent access.

func NewPseudoSeed

func NewPseudoSeed() int64

NewPseudoSeed generates a seed from crypto/rand.

func NewTestRand

func NewTestRand() (*rand.Rand, int64)

NewTestRand returns an instance of math/rand.Rand seeded from rng, which is seeded with the global seed. If the caller is a test with a different path-qualified name than the previous caller, rng is reseeded from the global seed. This rand.Rand is useful in testing to produce deterministic, reproducible behavior.

func NewTestRandWithSeed

func NewTestRandWithSeed(seed int64) *rand.Rand

NewTestRandWithSeed returns an instance of math/rand.Rand, similar to NewTestRand, but with the seed specified.

func RandBytes

func RandBytes(r *rand.Rand, size int) []byte

RandBytes returns a byte slice of the given length with random data.

func RandDuration

func RandDuration(r *rand.Rand, max time.Duration) time.Duration

RandDuration returns a random duration in [0, max).

func RandInt63InRange

func RandInt63InRange(r *rand.Rand, min, max int64) int64

RandInt63InRange returns a value in [min, max)

func RandIntInRange

func RandIntInRange(r *rand.Rand, min, max int) int

RandIntInRange returns a value in [min, max)

func RandString

func RandString(rng *rand.Rand, length int, alphabet string) string

RandString generates a random string of the desired length from the input alphabet. It is useful when you want to generate keys that would be printable without further escaping if alphabet is restricted to alphanumeric chars.

func RandUint64n

func RandUint64n(r *rand.Rand, n uint64) uint64

RandUint64n generates a 64-bit random number in [0, n) range. Note: n == 0 means n is math.MaxUint64 + 1

func ReadTestdataBytes

func ReadTestdataBytes(r *rand.Rand, arr []byte)

ReadTestdataBytes reads random bytes, but then nudges them into printable ASCII, *reducing their randomness* to make them a little friendlier for humans using them as testdata.

func SeedForTests

func SeedForTests()

SeedForTests seeds the random number generator and prints the seed value used. This function should be called from TestMain; individual tests should not touch the seed of the global random number generator.

Types

This section is empty.

Jump to

Keyboard shortcuts

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