
randx
Convenient random generation functions for Go, covering integers, floats, booleans, durations, strings, and slices.
CHINESE README
中文说明
Main Features
🎲 Range-Based Random: Generate random int, int64, uint64, float64 within a given range
⏱️ Duration Random: Generate random time.Duration between two bounds
🔤 String Generation: Generate random strings from customizable charsets (digits, hex, alphabet, etc.)
🔀 Shuffle: Shuffle strings and slices in place
🎯 Boolean & Rate: Random boolean with configurable rate/percentage
📋 Generic Choose: Pick a random element from any slice using generics
Installation
go get github.com/yylego/randx
Quick Start
package main
import (
"fmt"
"github.com/yylego/randx"
)
func main() {
// Random int in [10, 20)
fmt.Println(randx.IntBetween(10, 20))
// Random boolean
fmt.Println(randx.Boolean())
// Random string of 16 alphanumeric chars
fmt.Println(randx.AlphaDigits.New(16))
// Pick a random element from a slice
fruits := []string{"apple", "banana", "orange", "grape"}
fmt.Println(randx.Choose(fruits))
}
API
Numeric Functions
| Function |
Description |
IntBetween(a, b int) int |
Random int in [min, max) |
Int64Between(a, b int64) int64 |
Random int64 in [min, max) |
Uint64Between(a, b uint64) uint64 |
Random uint64 in [min, max) |
Float64Between(a, b float64) float64 |
Random float64 in [min, max) |
IntSign() int |
Returns +1 or -1 at random |
IntPerm(n int) []int |
Random sequence of [0, n) |
Boolean Functions
| Function |
Description |
Boolean() bool |
50/50 random boolean |
RateBoolean(n, v int) bool |
True with v/n chance |
PercentBoolean(v int) bool |
True with v% chance |
Duration Functions
| Function |
Description |
DurationBetween(a, b time.Duration) |
Random duration in [a, b) |
SecondsBetween(a, b int) |
Random seconds |
MillisBetween(a, b int) |
Random milliseconds |
MicrosBetween(a, b int) |
Random microseconds |
MinutesBetween(a, b int) |
Random minutes |
String & Slice Functions
| Function |
Description |
GenChars.New(size int) string |
Generate random string from charset |
GenChars.Shuffle() string |
Shuffle charset chars |
ShuffleString(s string) string |
Shuffle a string |
ShuffleSlice[T](a []T) |
Shuffle a slice in place |
Choose[T](a []T) T |
Pick a random element |
Predefined Charsets
| Constant |
Chars |
Digits |
0123456789 |
HexDigits |
0123456789ABCDEF |
Lowercase |
abcdefghijklmnopqrstuvwxyz |
Uppercase |
ABCDEFGHIJKLMNOPQRSTUVWXYZ |
Alphabet |
A-Z + a-z |
AlphaDigits |
A-Z + a-z + 0-9 |
📄 License
MIT License - see LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- 🐛 Mistake reports? Open an issue on GitHub with reproduction steps
- 💡 Fresh ideas? Create an issue to discuss
- 📖 Documentation confusing? Report it so we can improve
- 🚀 Need new features? Share the use cases to help us understand requirements
- ⚡ Performance issue? Help us optimize through reporting slow operations
- 🔧 Configuration problem? Ask questions about complex setups
- 📢 Follow project progress? Watch the repo to get new releases and features
- 🌟 Success stories? Share how this package improved the workflow
- 💬 Feedback? We welcome suggestions and comments
🔧 Development
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git).
- Navigate: Navigate to the cloned project (
cd repo-name)
- Branch: Create a feature branch (
git checkout -b feature/xxx).
- Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions
- Documentation: Update documentation to support client-facing changes
- Stage: Stage changes (
git add .)
- Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code
- Push: Push to the branch (
git push origin feature/xxx).
- PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
🌟 Support
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- ⭐ Give GitHub stars if this project helps you
- 🤝 Share with teammates and (golang) programming friends
- 📝 Write tech blogs about development tools and workflows - we provide content writing support
- 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! 🎉🎉🎉
GitHub Stars
