Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updated random function to give value between range
  • Loading branch information
haseth
haseth committed Mar 9, 2020
commit f6e23e7a4e1efb106403480dd71f2448ac4f5c2c
6 changes: 3 additions & 3 deletions random_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func RandText(size int, sourceChars string) string {
return string(text)
}

//Random get random in min between max. 生成指定大小的随机数.
func random(min int64, max int64) float64 {
return rand.Float64()*float64(max) - float64(min)
//Random get random number between min and max. 生成指定大小的随机数.
func Random(min int64, max int64) float64 {
return float64(min) + rand.Float64()*float64(max-min)
}

//RandDeepColor get random deep color. 随机生成深色系.
Expand Down
26 changes: 26 additions & 0 deletions random_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ func TestRandText(t *testing.T) {
})
}
}

func TestRandom(t *testing.T) {
type args struct {
min int64
max int64
}
tests := []struct {
name string
args args
}{
{"", args{-10, 10}},
{"", args{-1, 5}},
{"", args{0, 15}},
{"", args{10, 14}},
{"", args{10, 10}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Random(tt.args.min, tt.args.max)
// if out of bound then error
if got < float64(tt.args.min) || got > float64(tt.args.max) {
t.Errorf("RandText() = %v, out of range", got)
}
})
}
}