randutil

package
v0.35.7 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: Unlicense Imports: 7 Imported by: 0

Documentation

Overview

Package randutil contains utilities for random numbers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendString added in v0.35.3

func AppendString(orig []byte, rng *rand.Rand, l uint64) (res []byte)

AppendString appends a randomly-generated string of length l, containing only valid Unicode characters, to orig. rng must not be nil.

func AppendStringASCII added in v0.35.3

func AppendStringASCII(orig []byte, rng *rand.Rand, l uint64) (res []byte)

AppendStringASCII appends a randomly-generated string of length l, containing only printable ASCII characters, to orig. rng must not be nil.

func AppendStringAlphabet added in v0.35.3

func AppendStringAlphabet(orig []byte, rng *rand.Rand, runeLen uint64, ab Alphabet) (res []byte)

AppendStringAlphabet appends a randomly-generated string with runeLen runes, containing only characters from Alphabet ab, to orig.

func MustNewSeed

func MustNewSeed() (seed [32]byte)

MustNewSeed returns new 32-byte seed for pseudorandom generators.

func String added in v0.35.3

func String(rng *rand.Rand, l uint64) (s string)

String returns a randomly-generated string of length l, containing only valid Unicode characters. rng must not be nil.

Example
rng := rand.New(rand.NewChaCha8([32]byte{}))

for range 5 {
	fmt.Printf("%+q\n", randutil.String(rng, 16))
}
Output:

"\U000208b5\U00102b06\U000f1b5c\u458fo"
"\U000118d0\U000281b8\u1db7\U000280e1\x14"
"\U0010d7e9\U0001e869\u6a9d\ue194\u05e6"
"\ue91b\U0001f3a4\U000f720f\U000fe986{"
"\ud5e9\U00023c56\U000fedab\U000215f2="

func StringASCII added in v0.35.3

func StringASCII(rng *rand.Rand, l uint64) (s string)

StringASCII returns a randomly-generated string of length l, containing only printable ASCII characters. rng must not be nil.

Example
rng := rand.New(rand.NewChaCha8([32]byte{}))

for range 5 {
	fmt.Printf("%q\n", randutil.StringASCII(rng, 16))
}
Output:

"`G+KzB8t&I_XU!GE"
"5fX&@QEB.OeYp4_ "
"Z\\M=--'~I*H>=YA6"
"Y[:\"LU?h$GR:3i%f"
"UF4L*LXaOC\\O;Gvx"

func StringAlphabet added in v0.35.3

func StringAlphabet(rng *rand.Rand, l uint64, ab Alphabet) (s string)

StringAlphabet returns a randomly-generated string with at runeLen runes, containing only characters from Alphabet ab, to orig.

Example
rng := rand.New(rand.NewChaCha8([32]byte{}))
const ab = "1234"

for range 5 {
	fmt.Printf("%q\n", randutil.StringAlphabet(rng, 16, ab))
}
Output:

"2341214114341141"
"1224442324341133"
"4331211134343343"
"2424241144341434"
"3312423221343313"
Example (Emoji)
rng := rand.New(rand.NewChaCha8([32]byte{}))
const ab = "🌑🌒🌓🌔🌕🌖🌗🌘"

for range 5 {
	fmt.Printf("%q\n", randutil.StringAlphabet(rng, 16, ab))
}
Output:

"🌗🌗🌗🌒🌔🌔🌓🌘🌔🌑🌔🌑🌗🌗🌔🌓"
"🌗🌖🌗🌖🌘🌕🌒🌓🌕🌒🌗🌘🌗🌕🌔🌗"
"🌑🌔🌘🌑🌓🌖🌔🌑🌘🌖🌓🌘🌔🌑🌘🌔"
"🌑🌕🌖🌖🌑🌘🌔🌘🌕🌗🌒🌕🌓🌗🌘🌕"
"🌑🌘🌓🌔🌖🌓🌒🌓🌗🌑🌔🌕🌑🌕🌓🌑"

Types

type Alphabet added in v0.35.3

type Alphabet = string

Alphabet is an alias for strings containing alphabets for random-string generation. Alphabets must contain valid UTF-8. Alphabets should not contain duplicates. It is recommended that alphabets be sorted in the Unicode order.

const (
	// AlphabetNumbers contains only decimal digits.
	AlphabetNumbers Alphabet = "0123456789"

	// AlphabetLowercase contains only lowercase Latin letters.
	AlphabetLowercase Alphabet = "abcdefghijklmnopqrstuvwxyz"

	// AlphabetUppercase contains only uppercase Latin letters.
	AlphabetUppercase Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	// AlphabetBase64URLSafe contains characters used in URL-safe Base64
	// encodings.
	AlphabetBase64URLSafe Alphabet = "-" + AlphabetNumbers + AlphabetUppercase + "_" + AlphabetLowercase
)

type LockedSource

type LockedSource struct {
	// contains filtered or unexported fields
}

LockedSource is an implementation of rand.Source that is concurrency-safe.

func NewLockedSource

func NewLockedSource(src rand.Source) (s *LockedSource)

NewLockedSource returns new properly initialized *LockedSource.

func (*LockedSource) Uint64

func (s *LockedSource) Uint64() (r uint64)

Uint64 implements the rand.Source interface for *LockedSource.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader is a ChaCha8-based cryptographically strong random number reader that is safe for concurrent use.

func NewReader

func NewReader(seed [32]byte) (r *Reader)

NewReader returns a new properly initialized *Reader seeded with the given seed.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error. It's safe for concurrent use.

Jump to

Keyboard shortcuts

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