1 Star 0 Fork 0

szmaozi/go-utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
random.go 1.75 KB
Copy Edit Raw Blame History
szmaozi authored 2021-09-02 13:41 . wip: merge biob-v1
/*
* @Name:
* @Descripttion:
* @Warning:
* @version:
* @Author: moo
* @Date: 2021-01-03 18:05:41
* @LastEditors: moo
* @LastEditTime: 2021-01-30 15:39:44
*/
package utils
import (
// "github.com/olongfen/gen-id"
"bytes"
"fmt"
"math/rand"
"time"
"github.com/srlemon/gen-id/generator"
)
/***
https://github.com/olongfen/gen-id
go get github.com/olongfen/gen-id@master
***/
const char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func RandomName() string {
g := new(generator.GeneratorData)
return g.GeneratorName()
}
func RandomMobile() string {
g := new(generator.GeneratorData)
return g.GeneratorPhone()
}
func RandomInt(min int, max int) int {
if min >= max || max == 0 {
return max
}
time.Sleep(1)
rand.Seed(time.Now().UnixNano())
return min + rand.Intn(max-min)
}
func RandomTitle() string {
i := RandomInt(1, 99999)
s := fmt.Sprintf("标题[%05d]", i)
return s
}
func RandomSite() string {
i := RandomInt(1, 9999)
s := fmt.Sprintf("站点[%04d]", i)
return s
}
func RandomContent(size int) string {
a := make([]rune, size)
for i := range a {
a[i] = rune(RandomInt(19968, 40869))
}
return string(a)
}
func RandomString(size int) string {
rand.NewSource(time.Now().UnixNano()) // 产生随机种子
var s bytes.Buffer
for i := 0; i < size; i++ {
s.WriteByte(char[rand.Int63()%int64(len(char))])
}
return s.String()
}
func RandomTime() time.Time {
// min := time.Date(1970, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
min := time.Date(2021, 1, 3, 0, 0, 0, 0, time.Local).Unix()
max := time.Date(2021, 1, 8, 0, 0, 0, 0, time.Local).Unix()
delta := max - min
sec := rand.Int63n(delta) + min
return time.Unix(sec, 0)
}
func RandFloat(min, max float64) float64 {
res := min + rand.Float64()*(max-min)
return res
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/szmaozi/go-utils.git
git@gitee.com:szmaozi/go-utils.git
szmaozi
go-utils
go-utils
29e02a007caf

Search