random

package
v0.0.0-...-7b21f04 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package random provides utility functions for generating various types of random data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool() bool

Bool returns a pseudo-random boolean value.

func Digit

func Digit() string

Digit returns a single random numeric character (0-9) as a string.

func Digits

func Digits(length int) string

Digits generates a random string consisting only of numeric characters (0-9).

func Edit

func Edit(input string) (out string)

Edit applies a random modification to the input string that is nicely fit to diff comparison and patch creation. The function randomly performs one of six operations:

  • For multi-word strings (split by spaces): adds/removes entire words
  • For single-word strings: adds/removes individual characters

If input is empty, it generates a random title first.

func Element

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

Element returns a random element from the provided slice. If the slice is empty, it returns the zero value for type T.

func Email

func Email() string

Email generates randomly constructed email address.

func ImageAbstractPNG

func ImageAbstractPNG(whOpt ...int) ([]byte, error)

ImageAbstractPNG generates a random abstract image with gradients and shapes.

func ImagePNG

func ImagePNG(whOpt ...int) ([]byte, error)

ImagePNG generates an in-memory PNG image and returns its encoded bytes. By default, it creates a square image with random size between 100 and 500 pixels. Optional arguments allow overriding the dimensions:

  • no arguments: random square image (w == h)
  • one argument: square image with given width/height
  • two arguments: image with explicit width and height

The image content is a simple deterministic color gradient based on pixel coordinates, useful for tests, placeholders, or fixtures.

func Int

func Int(from, to int) int

Int generates a random integer within the specified inclusive range [min, max]. If max is less than min, the arguments are swapped to ensure the range is valid.

func Letter

func Letter() string

Letter returns a single random alphabetic character (a-z, A-Z) as a string.

func Letters

func Letters(length int) string

Letters generates a random string consisting only of alphabetic characters (a-z, A-Z).

func Maybe

func Maybe[T any](val T, probabilityOpt ...int) T

Maybe returns either the provided value or the zero value of T, based on the given probability.

func Patch

func Patch(input string) (patch string)

Patch applies an Edit to the input string and returns a patch.

func ReleaseDate

func ReleaseDate() string

ReleaseDate returns a randomly generated release date formatted using one of the allowed release date layouts.

func String

func String(lengthOpt ...int) string

String generates a random alphanumeric string. The length of the string defaults to 10 if no argument is provided.

func Title

func Title() string

Title generates a random string formatted in title case.

func URL

func URL() string

URL generates a random mocked HTTPS URL string.

func ValOrNil

func ValOrNil[T any](val T, probabilityOpt ...int) *T

ValOrNil returns a pointer to val or nil based on the given probability. The probability is specified in percent (0–100) and defaults to 50% if omitted.

Examples:

ValOrNil("hello")       // ~50% chance to return &"hello"
ValOrNil("hello", 10)   // 10% chance to return &"hello"
ValOrNil("hello", 100)  // always returns &"hello"
ValOrNil("hello", 0)    // always returns nil

Types

This section is empty.

Jump to

Keyboard shortcuts

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