Documentation
¶
Overview ¶
Package randutil contains utilities for random numbers.
Index ¶
- func AppendString(orig []byte, rng *rand.Rand, l uint64) (res []byte)
- func AppendStringASCII(orig []byte, rng *rand.Rand, l uint64) (res []byte)
- func AppendStringAlphabet(orig []byte, rng *rand.Rand, runeLen uint64, ab Alphabet) (res []byte)
- func MustNewSeed() (seed [32]byte)
- func String(rng *rand.Rand, l uint64) (s string)
- func StringASCII(rng *rand.Rand, l uint64) (s string)
- func StringAlphabet(rng *rand.Rand, l uint64, ab Alphabet) (s string)
- type Alphabet
- type LockedSource
- type Reader
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendString ¶ added in v0.35.3
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
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
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
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
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
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.