random

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SecureRng = rand.New(&cryptoRand{})

Functions

func MustSecureSlice

func MustSecureSlice[T ChoiceValue](length int, choices []T) []T

MustSecureSlice generates a random slice a given length and value choice set and ignore errors caused by the random source

func MustSecureString

func MustSecureString[T ~string](length int, choices T) string

MustSecureString generates a random string of a given length and value choice set and ignore errors caused by the random source

func SecureSlice

func SecureSlice[T ChoiceValue](length int, choices []T) ([]T, error)

SecureSlice generates a random slice of a given length and value choice set

func SecureString

func SecureString[T ~string](length int, choices T) (string, error)

SecureString generates a random string of a given length and value choice set

func Uint64

func Uint64() uint64

Uint64 generates a cryptographically secure random number

Types

type ChoiceValue

type ChoiceValue interface {
	~rune | ~byte
}

type Randomizer

type Randomizer[T any] struct {
	// contains filtered or unexported fields
}

Randomizer is an implementation of the alias method

func NewRandomizer

func NewRandomizer[T any]() *Randomizer[T]

NewRandomizer creates a Randomizer instance

func NewSecureRandomizer

func NewSecureRandomizer[T any]() *Randomizer[T]

NewSecureRandomizer creates a Randomizer instance that uses a cryptographically secure source for random number and sample generation

func (*Randomizer[T]) Add

func (r *Randomizer[T]) Add(weight float64, result T) error

Add adds elements to the Randomizer

func (*Randomizer[T]) AddElement

func (r *Randomizer[T]) AddElement(element *RandomizerElement[T]) error

AddElement adds elements to the Randomizer

func (*Randomizer[T]) AddMany

func (r *Randomizer[T]) AddMany(elements []RandomizerElement[T]) error

AddMany adds multiple elements to the Randomizer from an array of elements

func (*Randomizer[T]) MustSample

func (r *Randomizer[T]) MustSample() T

MustSample samples the alias method to get a random value, respecting the weights of each element

func (*Randomizer[T]) MustSampleMany

func (r *Randomizer[T]) MustSampleMany(count int) []T

MustSampleMany samples the alias method to get random values, respecting the weights of each element

func (*Randomizer[T]) Prepare

func (r *Randomizer[T]) Prepare()

Prepare the randomizer for sampling

func (*Randomizer[T]) Sample

func (r *Randomizer[T]) Sample() (T, error)

Sample the alias method to get a random value, respecting the weights of each element

func (*Randomizer[T]) SampleMany

func (r *Randomizer[T]) SampleMany(count int) ([]T, error)

SampleMany samples the alias method to get random values, respecting the weights of each element

type RandomizerElement

type RandomizerElement[T any] struct {
	Weight float64
	Result T
}

RandomizerElement is an element of a Randomizer

Jump to

Keyboard shortcuts

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