Documentation ¶
Overview ¶
Package random implements a variety of functions for generating random things which may or may not be useful.
Functions in here use the regular pseudo-random number generation found in "math/rand", and so shouldn't be assumed to be cryptographically safe unless otherwise specified. They also use the default RNG source and so may need to be seeded manually using rand.Seed.
Index ¶
- func Bool() bool
- func Bytes(length int) []byte
- func Color() colorful.HexColor
- func Colour() colorful.HexColor
- func Duration(lower, upper time.Duration) time.Duration
- func Element[T any](slice []T) T
- func Float(lower, upper float64) float64
- func Int(lower, upper int) int
- func WeightedElement[T any](choices []Choice[T]) T
- func Word() string
- type Choice
- type WordGenerator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Color ¶
func Color() colorful.HexColor
Color returns a random color, with 100% saturation and lightness.
func Int ¶
Int generates a random int with the given lower and upper bounds.
The lower and upper bounds are inclusive, so lower is the minimum number it could return and upper is the maximum number it could return.
func WeightedElement ¶
WeightedElement returns a random element from a slice of weighted choices.
func Word ¶
func Word() string
Word generates a randomised and (hopefully) semi-readable word, approximately 8 characters in length.
This is a wrapper around *WordGenerator.NewWord using a bias of 4. If longer or shorter words are required, use NewWordGenerator to create your own word generator.
Word will take longer on first run, since it doesn't initialise the word generator until it's used.
Types ¶
type WordGenerator ¶
type WordGenerator struct {
// contains filtered or unexported fields
}
WordGenerator is a random word generator.
func NewWordGenerator ¶
func NewWordGenerator() *WordGenerator
NewWordGenerator creates a new word generator, setting up the internal graph used to string characters together.
func (*WordGenerator) CanGenerate ¶
func (gen *WordGenerator) CanGenerate(s string) bool
CanGenerate returns true if the word generator is able to generate the given string.
Only lowercase strings with alphabetical characters are supported here.
func (*WordGenerator) NewWord ¶
func (gen *WordGenerator) NewWord(bias int) string
NewWord generates a randomised and (hopefully) semi-readable word.
The given bias is used to set the length of the word. This isn't measured in characters and just influences how many iterations it takes before the word generator stops and returns. In general, a word will be approximately (bias*2)±1 characters long. 2 to 5 is usually the sweet spot.
Note that there is no filtering done for profanity or offensive words, and so there is a non-zero chance that this method will return something horrible. Filtering should be done separately if this is desired, and I apologise in advance.