rand

package
v0.0.0-...-c778a73 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: MIT Imports: 6 Imported by: 0

README

Random (rand)

  • Int: Generates a pseudo-random integer.
  • Int64: Generates a pseudo-random 63-bit integer.
  • SecureNumber: Generates a cryptographically secure random number.
  • NumberInRange: Generates a random number within a specified range.
  • String: Generates a random alphanumeric string.
  • StringWithLength: Generates a random string of a custom length.
  • StringWithCharset: Generates a random string using a custom character set.
  • Pick: Picks a random element from a given slice.
  • Shuffle: Shuffles the elements of a slice randomly.

Examples:

For examples of each function, please checkout EXAMPLES.md


Documentation

Index

Constants

View Source
const (
	// DefaultCharset defines the default characters
	DefaultCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

	// DefaultLength defines the default length for random string
	DefaultLength = 10
)

Variables

This section is empty.

Functions

func Int

func Int() int

Int returns a pseudo-random integer

func Int64

func Int64() int64

Int64 returns a pseudo-random 63-bit integer

func NumberInRange

func NumberInRange(min, max int64) (int64, error)

NumberInRange generates a pseudo-random number between min and max

func Pick

func Pick[T any](slice []T) (T, error)

Pick returns a random element from the provided slice

func SecureNumber

func SecureNumber() (int64, error)

SecureNumber returns a cryptographically secure random number. Note: This function is significantly slower than Int() and Int64() due to the use of crypto/rand.

func Shuffle

func Shuffle[T any](slice []T) error

Shuffle reorders the elements in the provided slice

func String

func String() (string, error)

String generates a random string using the default constants

func StringWithCharset

func StringWithCharset(length int, charset string) (string, error)

StringWithCharset generates a random string with the specified length and character set

func StringWithLength

func StringWithLength(length int) (string, error)

StringWithLength generates a random string of the specified length using the default charset

Types

This section is empty.

Jump to

Keyboard shortcuts

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