Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tim1116 committed Jan 30, 2022
1 parent b01cd7b commit a9c53d6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
11 changes: 6 additions & 5 deletions slice/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ import (
"time"
)

type RandIntSlice struct {
type RandSlice struct {
Min, Max int64
Len int
}

// RandIntSlice 返回随机int类型切片
func (r RandIntSlice) createIntSlice() []int {
// CreateIntSlice 返回随机int类型切片
// 切片中的元素范围 [Min,Max]
func (r RandSlice) CreateIntSlice() []int {
var res = make([]int, r.Len)
for i := 0; i < r.Len; i++ {
res[i] = int(util.RangeRand(r.Min, r.Max))
}
return res
}

// createRandomIntSlice 生成长度为len的int类型随机切片
// CreateRandomIntSlice 生成长度为len的int类型随机切片
// 切片中元素的最大值为max
func createRandomIntSlice(len int, max int) []int {
func CreateRandomIntSlice(len int, max int) []int {
var res = make([]int, len)
rand.Seed(time.Now().UnixNano())
for i := 0; i < len; i++ {
Expand Down
26 changes: 19 additions & 7 deletions slice/create_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package slice

import (
"fmt"
"testing"
)

func TestCreateRandomIntSlice(t *testing.T) {
createRandomIntSlice(5, 10)
lenSlice := 5
sli := CreateRandomIntSlice(lenSlice, 10)
if len(sli) != lenSlice {
t.Error(`切片长度异常`)
}
}

func TestCreateIntSlice(t *testing.T) {
rand := RandIntSlice{
Min: -1,
Max: 50,
Len: 10,
vMax, vMin, vLen := 50, -1, 10
rand := RandSlice{
Min: int64(vMin),
Max: int64(vMax),
Len: vLen,
}
intSlice := rand.CreateIntSlice()
if len(intSlice) != vLen {
t.Error(`切片长度异常`)
}
for _, v := range intSlice {
if v < vMin || v > vMax {
t.Error(`范围异常`)
}
}
fmt.Println(rand.createIntSlice())
}
16 changes: 3 additions & 13 deletions util/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@ package util

import (
"crypto/rand"
"math"
"math/big"
)

// RangeRand 生成区间[-m, n]的安全随机数
// RangeRand 生成区间[-m, n]的 int64安全随机数
func RangeRand(min, max int64) int64 {
if min > max {
panic("the min is greater than max!")
}

if min < 0 {
f64Min := math.Abs(float64(min))
i64Min := int64(f64Min)
result, _ := rand.Int(rand.Reader, big.NewInt(max+1+i64Min))

return result.Int64() - i64Min
} else {
result, _ := rand.Int(rand.Reader, big.NewInt(max-min+1))
return min + result.Int64()
}
result, _ := rand.Int(rand.Reader, big.NewInt(max-min+1))
return min + result.Int64()
}
18 changes: 18 additions & 0 deletions util/rand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package util

import "testing"

func TestRangeRand(t *testing.T) {
num := RangeRand(10, 10)
if num != 10 {
t.Error(`RangeRand 异常`)
}
for i := 0; i < 100; i++ {
num = RangeRand(1, 10)
if num < 1 {
t.Error(`RangeRand 异常`)
} else if num > 10 {
t.Error(`RangeRand 异常`)
}
}
}

0 comments on commit a9c53d6

Please sign in to comment.