xrand

package module
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 5 Imported by: 1

README

Xrand

Build Status License Coverage Status Go Reference


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. See go doc reference for more details and examples.

Misc

Feel free to use this pkg if you like it and fits your needs.
As it is a light/lite pkg, you can also just copy-paste the code, instead of importing it, keeping the license header.

License

This package is released under a MIT license. See LICENSE.

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

View Source
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)
}

func Intn

func Intn(n int) int

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)
}

func IntnBetween

func IntnBetween(min, max int) int

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)
}

func Jitter

func Jitter(duration time.Duration, maxFactor ...float64) time.Duration

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)
}

func String

func String(n int, alphabet ...string) 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)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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