random

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package random provides cryptographically secure random utilities:

  • Secure random numbers (crypto/rand): Int, IntRange, Float64, Bytes
  • Random strings with custom charsets: String, StringWithCharset
  • Sampling & shuffling: Shuffle, Sample, Choice, Permutation
  • Random colors: HexColor, RGBColor, HSLColor
  • UUID v4 (RFC 4122)

All functions use crypto/rand and are safe for concurrent use.

Index

Constants

View Source
const (
	CharsetAlphaLower = "abcdefghijklmnopqrstuvwxyz"
	CharsetAlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	CharsetAlpha      = CharsetAlphaLower + CharsetAlphaUpper
	CharsetNumeric    = "0123456789"
	CharsetAlphaNum   = CharsetAlpha + CharsetNumeric
	CharsetHexLower   = "0123456789abcdef"
	CharsetHexUpper   = "0123456789ABCDEF"
	CharsetBase62     = CharsetAlphaNum
	CharsetBase64URL  = CharsetAlphaNum + "-_"
	CharsetSymbol     = "!@#$%^&*()-_=+[]{}|;:,.<>?"
)

Predefined character sets for random string generation.

Variables

This section is empty.

Functions

func Base64URLString

func Base64URLString(n int) string

Base64URLString returns a random URL-safe base64 string of length n.

func Bool

func Bool() bool

Bool returns a cryptographically secure random bool.

func Bytes

func Bytes(n int) []byte

Bytes returns n cryptographically secure random bytes.

func Choice

func Choice[T any](s []T) T

Choice returns a random element from a slice. Panics if the slice is empty.

func Choices

func Choices[T any](s []T, k int) []T

Choices returns k random elements from a slice (with replacement). Panics if the slice is empty or k < 0.

func Float64

func Float64() float64

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

func Float64Range

func Float64Range(min, max float64) float64

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

func HexColor

func HexColor() string

HexColor returns a random hex color string (e.g. "#a3b9f2").

func HexString

func HexString(n int) string

HexString returns a random lowercase hex string of length n.

func Int

func Int() int

Int returns a cryptographically secure random non-negative int.

func Int64

func Int64() int64

Int64 returns a cryptographically secure random int64 in [0, 1<<62).

func Int64Range

func Int64Range(min, max int64) int64

Int64Range returns a cryptographically secure random int64 in [min, max]. Panics if min > max.

func Int64n

func Int64n(n int64) int64

Int64n returns a cryptographically secure random int64 in [0, n). Panics if n <= 0.

func IntRange

func IntRange(min, max int) int

IntRange returns a cryptographically secure random int in [min, max]. Panics if min > max.

func Intn

func Intn(n int) int

Intn returns a cryptographically secure random int in [0, n). Panics if n <= 0.

func LowerString

func LowerString(n int) string

LowerString returns a random lowercase-alpha string of length n.

func MathIntn

func MathIntn(n int) int

MathIntn returns a non-cryptographic random int in [0, n) using math/rand/v2. Faster than Intn but not secure.

func MathShuffle

func MathShuffle[T any](s []T)

MathShuffle shuffles a slice using math/rand/v2 (faster, non-crypto). Useful when cryptographic security is not required.

func NumericString

func NumericString(n int) string

NumericString returns a random numeric string of length n.

func Password

func Password(n int) string

Password returns a random password of length n containing at least one lowercase, one uppercase, one digit, and one symbol. Panics if n < 4.

func Permutation

func Permutation(n int) []int

Permutation returns a random permutation of [0, n) using crypto/rand.

func Read

func Read(b []byte) (int, error)

Read fills b with cryptographically secure random bytes. It is a drop-in replacement for crypto/rand.Read.

func Sample

func Sample[T any](s []T, k int) []T

Sample returns k unique random elements from a slice (without replacement). Panics if k > len(s) or k < 0 or s is empty.

func Shuffle

func Shuffle(n int, swap func(i, j int))

Shuffle shuffles a slice in-place using a swap function. The swap function is called for indices i and j.

func ShuffleBytes

func ShuffleBytes(s []byte)

ShuffleBytes shuffles a byte slice in-place using crypto/rand.

func ShuffleInts

func ShuffleInts(s []int)

ShuffleInts shuffles an int slice in-place using crypto/rand.

func String

func String(n int) string

String returns a random string of length n using CharsetAlphaNum.

func StringWithCharset

func StringWithCharset(n int, charset string) string

StringWithCharset returns a random string of length n using the given charset. Panics if charset is empty or n < 0.

func UUID

func UUID() string

UUID returns a random UUID v4 string in canonical form (e.g. "550e8400-e29b-41d4-a716-446655440000").

func UUIDBytes

func UUIDBytes() [16]byte

UUIDBytes returns 16 random bytes formatted as a UUID v4. The returned bytes have the version and variant bits set.

func UUIDNoDashes

func UUIDNoDashes() string

UUIDNoDashes returns a random UUID v4 string without dashes (32 hex characters).

func UpperString

func UpperString(n int) string

UpperString returns a random uppercase-alpha string of length n.

Types

type HSL

type HSL struct{ H, S, L float64 }

HSL represents an HSL color with H in [0, 360), S/L in [0, 1].

func HSLColor

func HSLColor() HSL

HSLColor returns a random HSL color with good visual distribution. Hue is uniform [0, 360), saturation and lightness are in comfortable ranges for pleasing colors.

type RGB

type RGB struct{ R, G, B uint8 }

RGB represents an RGB color with values 0-255.

func HSLToRGB

func HSLToRGB(hsl HSL) RGB

HSLToRGB converts an HSL color to RGB.

func RGBColor

func RGBColor() RGB

RGBColor returns a random RGB color.

func (RGB) Hex

func (c RGB) Hex() string

Hex returns the hex color string (e.g. "#a3b9f2").

Jump to

Keyboard shortcuts

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