From 0550aec6ad622c7217f141fa02ab8b140ef17cd6 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Sat, 4 Jul 2026 01:53:00 +0100 Subject: [PATCH] rework --- README.md | 4 ++-- random.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9ca7838..c387049 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@ package main import ( "fmt" - "github.com/annybs/go/random" + "code.aneur.in/go/random" ) func main() { n := make([]bool, 10) for i := range n { - fmt.Println(random.Str(4 + i)) + fmt.Println(random.Alpha(4 + i)) } } ``` diff --git a/random.go b/random.go index 6880f40..3d47ebc 100644 --- a/random.go +++ b/random.go @@ -1,9 +1,56 @@ package random -import "math/rand" +import ( + "math/rand" + "strings" +) -// Str generates a random string of the specified length using an alphanumeric character set. -var Str = String([]rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")) +// Standard character set. +const ( + CharsetAlpha string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + CharsetAlphanumeric string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + CharsetAlphaLower string = "abcdefghijklmnopqrstuvwxyz" + CharsetAlphaUpper string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + CharsetNumeric string = "0123456789" + CharsetSymbols string = "!@#$%^&*()-=_+[]{};:,.<>/?" +) + +// Alpha generates a random string of the specified length using a mixed-case alphabetical character set. +var Alpha = String([]rune(CharsetAlpha)) + +// Alphanumeric generates a random string of the specified length using an alphanumeric character set. +var Alphanumeric = String([]rune(CharsetAlphanumeric)) + +// AlphaLower generates a random string of the specified length using a lowercase alphabetical character set. +var AlphaLower = String([]rune(CharsetAlphaLower)) + +// AlphaUpper generates a random string of the specified length using an uppercase alphabetical character set. +var AlphaUpper = String([]rune(CharsetAlphaUpper)) + +// Numeric generates a random string of the specified length containing numbers only. +var Numeric = String([]rune(CharsetNumeric)) + +// Password generates a random string of the specified length, including the specified counts of numbers and symbols. +func Password(length, numbers, symbols int) string { + parts := []string{ + Alpha(length - numbers - symbols), + Numeric(numbers), + Symbols(symbols), + } + + str := strings.Join(parts, "") + m := map[int]rune{} + for i, c := range str { + m[i] = c + } + + jumbled := []rune{} + for _, c := range m { + jumbled = append(jumbled, c) + } + + return string(jumbled) +} // String returns a function that can be used to generate a random string of the specified length using the given character set. func String(chars []rune) func(length int) string { @@ -16,3 +63,6 @@ func String(chars []rune) func(length int) string { return string(s) } } + +// Symbols generates a random string of the specified length using a limited selection of symbols. +var Symbols = String([]rune(CharsetSymbols))