代码拉取完成,页面将自动刷新
package str
import (
"crypto/rand"
"encoding/binary"
"time"
)
var (
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
symbols = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" // 32
digits = "0123456789" // 10
characters = letters + digits + symbols // 94
)
const (
// Buffer size for uint32 random number.
bufferChanSize = 10000
)
var (
// bufferChan is the buffer for random bytes,
// every item storing 4 bytes.
bufferChan = make(chan []byte, bufferChanSize)
)
func init() {
go asyncProducingRandomBufferBytesLoop()
}
// asyncProducingRandomBufferBytes is a named goroutine, which uses an asynchronous goroutine
// to produce the random bytes, and a buffer chan to store the random bytes.
// So it has high performance to generate random numbers.
func asyncProducingRandomBufferBytesLoop() {
var step int
for {
buffer := make([]byte, 1024)
if n, err := rand.Read(buffer); err != nil {
panic(err)
} else {
// The random buffer from system is very expensive,
// so fully reuse the random buffer by changing
// the step with a different number can
// improve the performance a lot.
// for _, step = range []int{4, 5, 6, 7} {
for _, step = range []int{4} {
for i := 0; i <= n-4; i += step {
bufferChan <- buffer[i : i+4]
}
}
}
}
}
func Intn(max int) int {
if max <= 0 {
return max
}
n := int(binary.LittleEndian.Uint32(<-bufferChan)) % max
if (max > 0 && n < 0) || (max < 0 && n > 0) {
return -n
}
return n
}
// B retrieves and returns random bytes of given length `n`.
func B(n int) []byte {
if n <= 0 {
return nil
}
i := 0
b := make([]byte, n)
for {
copy(b[i:], <-bufferChan)
i += 4
if i >= n {
break
}
}
return b
}
// N returns a random int between min and max: [min, max].
// The `min` and `max` also support negative numbers.
func N(min, max int) int {
if min >= max {
return min
}
if min >= 0 {
return Intn(max-min+1) + min
}
// As `Intn` dose not support negative number,
// so we should first shift the value to right,
// then call `Intn` to produce the random number,
// and finally shift the result back to left.
return Intn(max+(0-min)+1) - (0 - min)
}
// S returns a random str which contains digits and letters, and its length is `n`.
// The optional parameter `symbols` specifies whether the result could contain symbols,
// which is false in default.
func S(n int, symbols ...bool) string {
if n <= 0 {
return ""
}
var (
b = make([]byte, n)
numberBytes = B(n)
)
for i := range b {
if len(symbols) > 0 && symbols[0] {
b[i] = characters[numberBytes[i]%94]
} else {
b[i] = characters[numberBytes[i]%62]
}
}
return string(b)
}
// D returns a random time.Duration between min and max: [min, max].
func D(min, max time.Duration) time.Duration {
multiple := int64(1)
if min != 0 {
for min%10 == 0 {
multiple *= 10
min /= 10
max /= 10
}
}
n := int64(N(int(min), int(max)))
return time.Duration(n * multiple)
}
// String randomly picks and returns `n` count of chars from given str `s`.
// It also supports unicode str like Chinese/Russian/Japanese, etc.
func String(s string, n int) string {
if n <= 0 {
return ""
}
var (
b = make([]rune, n)
runes = []rune(s)
)
if len(runes) <= 255 {
numberBytes := B(n)
for i := range b {
b[i] = runes[int(numberBytes[i])%len(runes)]
}
} else {
for i := range b {
b[i] = runes[Intn(len(runes))]
}
}
return string(b)
}
// Digits returns a random str which contains only digits, and its length is `n`.
func Digits(n int) string {
if n <= 0 {
return ""
}
var (
b = make([]byte, n)
numberBytes = B(n)
)
for i := range b {
b[i] = digits[numberBytes[i]%10]
}
return string(b)
}
// Letters returns a random str which contains only letters, and its length is `n`.
func Letters(n int) string {
if n <= 0 {
return ""
}
var (
b = make([]byte, n)
numberBytes = B(n)
)
for i := range b {
b[i] = letters[numberBytes[i]%52]
}
return string(b)
}
// Symbols returns a random str which contains only symbols, and its length is `n`.
func Symbols(n int) string {
if n <= 0 {
return ""
}
var (
b = make([]byte, n)
numberBytes = B(n)
)
for i := range b {
b[i] = symbols[numberBytes[i]%32]
}
return string(b)
}
// Perm returns, as a slice of n int numbers, a pseudo-random permutation of the integers [0,n).
// TODO performance improving for large slice producing.
func Perm(n int) []int {
m := make([]int, n)
for i := 0; i < n; i++ {
j := Intn(i + 1)
m[i] = m[j]
m[j] = i
}
return m
}
// Meet randomly calculate whether the given probability `num`/`total` is met.
func Meet(num, total int) bool {
return Intn(total) < num
}
// MeetProb randomly calculate whether the given probability is met.
func MeetProb(prob float32) bool {
return Intn(1e7) < int(prob*1e7)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。