random

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool() bool

Bool returns a random boolean.

func Float64

func Float64() float64

Float64 returns a random float64 in [0.0, 1.0).

func Float64Range

func Float64Range(min, max float64) float64

Float64Range returns a random float64 in [min, max).

func GenRandomBytes

func GenRandomBytes(n int) ([]byte, error)

GenRandomBytes generates n cryptographically random bytes. Returns an error if n < 0.

func GenRandomHashKey

func GenRandomHashKey() string

GenRandomHashKey generates a cryptographically random SHA-256 hash key.

func GenRandomHex

func GenRandomHex(byteLen int) (string, error)

GenRandomHex generates a random hex string of the given byte length.

func IntN

func IntN(n int) int

IntN returns a random int in [0, n).

func IntRange

func IntRange(min, max int) int

IntRange returns a random int in [min, max].

func PickN

func PickN[T any](items []T, min, max int) []T

PickN picks between min and max (inclusive) unique elements from items. If max > len(items), it is clamped to len(items).

func PickOne

func PickOne[T any](items []T) T

PickOne picks one random element from items. Panics if items is empty.

func PickOneOr

func PickOneOr[T any](items []T, fallback T) T

PickOneOr picks a random element from items, returning fallback if items is empty.

func Sample

func Sample[T any](items []T, n int) []T

Sample picks n unique elements from items using reservoir sampling. If n >= len(items), returns a shuffled copy of items.

func Shuffle

func Shuffle[T any](items []T) []T

Shuffle returns a new slice with elements in random order. The original slice is not modified.

func String

func String(length int, charset string) string

String generates a random string of the given length using the specified charset. If charset is empty, uses alphanumeric characters. Returns empty string if length <= 0.

func WeightedPick

func WeightedPick[T any](items []T, weights []float64) T

WeightedPick picks one element from items based on weights. Panics if items and weights have different lengths or if all weights are zero.

Types

This section is empty.

Jump to

Keyboard shortcuts

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