random

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

package random provides cryptographic sourced random utility functions.

Index

Constants

This section is empty.

Variables

View Source
var ErrChoiceIsEmpty = errors.New("can't choose from an empty slice")

ErrChoiceIsEmpty is returned if the user attempts to choose from an empty slice.

Functions

func Choice

func Choice[S ~[]E, E any](s S) (E, error)

Choice returns a random element from the given slice.

func Integer

func Integer[T integer](mn, mx T) (T, error)

Integer returns an integer in [mn..mx].

func Selection added in v1.2.0

func Selection[S ~[]E, E any](s S, n int) (S, error)

Selection returns a slice of 'n' random entries from the given slice.

NOTE: This should not be used for password generation.

func WeightedChoice

func WeightedChoice[T any](s []WeightedChoiceOption[T]) (T, error)

WeightedChoice returns an element from the given slice of options where elements with a higher weight are more likely to be selected.

Types

type WeightedChoiceOption

type WeightedChoiceOption[T any] struct {
	// Weight of the option, a higher weight means it's more likely to be selected.
	//
	// NOTE: The sum of the overall weights must be less than 'math.MaxInt'.
	Weight uint

	// Option that may be picked.
	Option T
}

WeightedChoiceOption pairs a type with a weight.

Jump to

Keyboard shortcuts

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