Compare commits

...

3 Commits

Author SHA1 Message Date
dtapps aae6d7220f - update
2 months ago
dtapps e413b27998 增加 `Range` 方法
5 months ago
李光春 026956447d - update
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago

4
.gitignore vendored

@ -4,6 +4,6 @@
.idea
.vscode
*.log
goinit.sh
gomod.sh
*_test.go
go_mod.sh
/vendor/

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 李光春
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -0,0 +1,17 @@
<h1>
<a href="https://www.dtapp.net/">Golang</a>
</h1>
📦 Golang Random
[comment]: <> (go)
[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gorandom?status.svg)](https://pkg.go.dev/go.dtapp.net/gorandom)
[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gorandom/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gorandom)
[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gorandom)](https://goreportcard.com/report/go.dtapp.net/gorandom)
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgorandom)
#### 安装
```go
go get -v -u go.dtapp.net/gorandom@v1.0.3
```

@ -1,3 +1,3 @@
module go.dtapp.net/gorandom
go 1.18
go 1.22

@ -1,7 +1,7 @@
package gorandom
import (
"math/rand"
"math/rand/v2"
"time"
)
@ -15,7 +15,11 @@ func random[T int | int64](n T, chars string) string {
if n <= 0 {
return ""
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// 随机数生成器,加入时间戳保证每次生成的随机数不一样
seed := time.Now().UnixNano() // rand内部运算的随机数
r := rand.New(rand.NewPCG(uint64(seed), uint64(seed))) // rand计算得到的随机数
bytes := make([]byte, n, n)
l := len(chars)
var i T = 0
@ -23,7 +27,7 @@ func random[T int | int64](n T, chars string) string {
if i >= n {
break
}
bytes[i] = chars[r.Intn(l)]
bytes[i] = chars[r.IntN(l)]
i++
}
return string(bytes)
@ -48,3 +52,19 @@ func Numeric[T int | int64](n T) string {
func Ascii[T int | int64](n T) string {
return random(n, ascii)
}
func Range(min int64, max int64) int64 {
var number int64
for {
// 生成在范围 [min, max) 内的随机数
number = int64(rand.IntN(int(max)-int(min)) + int(min))
// 检查随机数是否大于 min 且小于 max
if number > min && number < max {
break
}
}
return number
}

@ -1,3 +1,3 @@
package gorandom
const Version = "1.0.0"
const Version = "1.0.3"

Loading…
Cancel
Save