Documentation
¶
Overview ¶
Package xrand contains some randomize utilities like a random string generator, a jitter function for durations. It uses math rand seeded with a crypro rand generated seed.
Index ¶
Examples ¶
Constants ¶
const ( // AlphanumAlphabet consists of Ascii lowercase letters, and digits. AlphanumAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789" // DigitsAlphabet consists of 1..9 numbers. DigitsAlphabet = "0123456789" )
Variables ¶
This section is empty.
Functions ¶
func Float64 ¶
func Float64() float64
Float64 generates a random float64 in range [0.0, 1.0).
Example ¶
package main import ( "fmt" "github.com/actforgood/xrand" ) func main() { // generate a random float in [0.0, 1.0) randFloat := xrand.Float64() fmt.Println(randFloat) }
Output:
func Intn ¶
Intn generates a random integer in range [0,n). It panics if max <= 0.
Example ¶
package main import ( "fmt" "github.com/actforgood/xrand" ) func main() { // generate a random int in [0, 1000) randInt := xrand.Intn(1000) fmt.Println(randInt) }
Output:
func IntnBetween ¶
IntnBetween generates a random integer in range [min,max). It panics if max <= 0.
Example ¶
package main import ( "fmt" "github.com/actforgood/xrand" ) func main() { // generate a random int in [100, 200) randInt := xrand.IntnBetween(100, 200) fmt.Println(randInt) }
Output:
func Jitter ¶
Jitter returns a time.Duration altered with a random factor. This allows clients to avoid converging on periodic behaviour. If maxFactor is <= 0.0, a suggested default value will be chosen.
Example ¶
package main import ( "fmt" "time" "github.com/actforgood/xrand" ) func main() { // slightly alter +/- a time.Duration cacheTTL := 10 * time.Minute factor := 0.1 jitteredCacheTTL := xrand.Jitter(cacheTTL, factor) fmt.Println(jitteredCacheTTL) }
Output:
func String ¶
String generates a random string of length n with letters from the alphabet. Alphabet is optional and defaults to AlphanumAlphabet if not provided.
Example ¶
package main import ( "fmt" "github.com/actforgood/xrand" ) func main() { // generate a random string of length 16, containing [a-z0-9] letters. randString := xrand.String(16, xrand.AlphanumAlphabet) fmt.Println(randString) }
Output:
Types ¶
This section is empty.