1 Star 1 Fork 0

ytsunlei/utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
random.go 4.60 KB
一键复制 编辑 原始数据 按行查看 历史
lei05.sun 提交于 2018-11-01 10:03 . 修改包结构
package utils
import (
"math"
"math/rand"
"time"
)
//随机算法
var (
defaultRand = rand.New(rand.NewSource(time.Now().UnixNano()))
)
// Creates a random string based on a variety of options, using
// supplied source of randomness.
//
// If start and end are both 0, start and end are set
// to ' ' and 'z', the ASCII printable
// characters, will be used, unless letters and numbers are both
// false, in which case, start and end are set to 0 and math.MaxInt32.
//
// If set is not nil, characters between start and end are chosen.
//
// This method accepts a user-supplied rand.Rand
// instance to use as a source of randomness. By seeding a single
// rand.Rand instance with a fixed seed and using it for each call,
// the same random sequence of strings can be generated repeatedly
// and predictably.
func RandomSpec0(count uint, start, end int, letters, numbers bool,
chars []rune, rand *rand.Rand) string {
if count == 0 {
return ""
}
if start == 0 && end == 0 {
end = 'z' + 1
start = ' '
if !letters && !numbers {
start = 0
end = math.MaxInt32
}
}
buffer := make([]rune, count)
gap := end - start
for count != 0 {
count--
var ch rune
if len(chars) == 0 {
ch = rune(rand.Intn(gap) + start)
} else {
ch = chars[rand.Intn(gap)+start]
}
if letters && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) ||
numbers && (ch >= '0' && ch <= '9') ||
(!letters && !numbers) {
if ch >= rune(56320) && ch <= rune(57343) {
if count == 0 {
count++
} else {
buffer[count] = ch
count--
buffer[count] = rune(55296 + rand.Intn(128))
}
} else if ch >= rune(55296) && ch <= rune(56191) {
if count == 0 {
count++
} else {
// high surrogate, insert low surrogate before putting it in
buffer[count] = rune(56320 + rand.Intn(128))
count--
buffer[count] = ch
}
} else if ch >= rune(56192) && ch <= rune(56319) {
// private high surrogate, no effing clue, so skip it
count++
} else {
buffer[count] = ch
}
} else {
count++
}
}
return string(buffer)
}
// Creates a random string whose length is the number of characters specified.
//
// Characters will be chosen from the set of alpha-numeric
// characters as indicated by the arguments.
//
// Param count - the length of random string to create
// Param start - the position in set of chars to start at
// Param end - the position in set of chars to end before
// Param letters - if true, generated string will include
// alphabetic characters
// Param numbers - if true, generated string will include
// numeric characters
func RandomSpec1(count uint, start, end int, letters, numbers bool) string {
return RandomSpec0(count, start, end, letters, numbers, nil, defaultRand)
}
// Creates a random string whose length is the number of characters specified.
//
// Characters will be chosen from the set of alpha-numeric
// characters as indicated by the arguments.
//
// Param count - the length of random string to create
// Param letters - if true, generated string will include
// alphabetic characters
// Param numbers - if true, generated string will include
// numeric characters
func RandomAlphaOrNumeric(count uint, letters, numbers bool) string {
return RandomSpec1(count, 0, 0, letters, numbers)
}
func RandomString(count uint) string {
return RandomAlphaOrNumeric(count, false, false)
}
func RandomStringSpec0(count uint, set []rune) string {
return RandomSpec0(count, 0, len(set)-1, false, false, set, defaultRand)
}
func RandomStringSpec1(count uint, set string) string {
return RandomStringSpec0(count, []rune(set))
}
// Creates a random string whose length is the number of characters
// specified.
//
// Characters will be chosen from the set of characters whose
// ASCII value is between 32 and 126 (inclusive).
func RandomAscii(count uint) string {
return RandomSpec1(count, 32, 127, false, false)
}
// Creates a random string whose length is the number of characters specified.
// Characters will be chosen from the set of alphabetic characters.
func RandomAlphabetic(count uint) string {
return RandomAlphaOrNumeric(count, true, false)
}
// Creates a random string whose length is the number of characters specified.
// Characters will be chosen from the set of alpha-numeric characters.
func RandomAlphanumeric(count uint) string {
return RandomAlphaOrNumeric(count, true, true)
}
// Creates a random string whose length is the number of characters specified.
// Characters will be chosen from the set of numeric characters.
func RandomNumeric(count uint) string {
return RandomAlphaOrNumeric(count, false, true)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ytsunlei/utils.git
git@gitee.com:ytsunlei/utils.git
ytsunlei
utils
utils
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385