You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1004 B
51 lines
1004 B
2 years ago
|
package gorandom
|
||
|
|
||
|
import (
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const numbers string = "0123456789"
|
||
|
const letters string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||
|
const specials = "~!@#$%^*()_+-=[]{}|;:,./<>?"
|
||
|
const alphanumerics string = letters + numbers
|
||
|
const ascii string = alphanumerics + specials
|
||
|
|
||
2 years ago
|
func random[T int | int64](n T, chars string) string {
|
||
2 years ago
|
if n <= 0 {
|
||
|
return ""
|
||
|
}
|
||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||
|
bytes := make([]byte, n, n)
|
||
|
l := len(chars)
|
||
2 years ago
|
var i T = 0
|
||
|
for {
|
||
|
if i >= n {
|
||
|
break
|
||
|
}
|
||
2 years ago
|
bytes[i] = chars[r.Intn(l)]
|
||
2 years ago
|
i++
|
||
2 years ago
|
}
|
||
|
return string(bytes)
|
||
|
}
|
||
|
|
||
|
// Alphanumeric 随机字母数字
|
||
2 years ago
|
func Alphanumeric[T int | int64](n T) string {
|
||
2 years ago
|
return random(n, alphanumerics)
|
||
|
}
|
||
|
|
||
|
// Alphabetic 随机字母
|
||
2 years ago
|
func Alphabetic[T int | int64](n T) string {
|
||
2 years ago
|
return random(n, letters)
|
||
|
}
|
||
|
|
||
|
// Numeric 随机数字
|
||
2 years ago
|
func Numeric[T int | int64](n T) string {
|
||
2 years ago
|
return random(n, numbers)
|
||
|
}
|
||
|
|
||
|
// Ascii 随机ASCII
|
||
2 years ago
|
func Ascii[T int | int64](n T) string {
|
||
2 years ago
|
return random(n, ascii)
|
||
|
}
|