random

package module
v0.0.0-...-c51b76d Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 8 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool() bool

Bool generates a random bool.

func Bytes

func Bytes(length int) []byte

Bytes generates a cryptographically-safe slice of bytes with the given length.

func Color

func Color() colorful.HexColor

Color returns a random color, with 100% saturation and lightness.

func Colour

func Colour() colorful.HexColor

Colour is an alias for Color.

func Duration

func Duration(lower, upper time.Duration) time.Duration

Duration generates a random duration with the given lower and upper bounds.

func Element

func Element[T any](slice []T) T

Element returns a random element from a slice.

func Float

func Float(lower, upper float64) float64

Float generates a random float with the given lower and upper bounds.

func Int

func Int(lower, upper int) 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

func WeightedElement[T any](choices []Choice[T]) T

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 Choice

type Choice[T any] struct {
	Weight int
	Value  T
}

Choice is a weighted value for use in WeightedElement.

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.

Jump to

Keyboard shortcuts

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