Skip to content

Commit a0d97cf

Browse files
committed
Merge branch 'rc' into v2
2 parents ab89f0a + 1f6bab4 commit a0d97cf

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

random/random.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,24 @@ func RandFloat(min, max float64, precision int) float64 {
126126

127127
n := rand.Float64()*(max-min) + min
128128

129-
return mathutil.RoundToFloat(n, precision)
129+
return mathutil.FloorToFloat(n, precision)
130130
}
131131

132132
// RandFloats generate a slice of random float64 numbers of length that do not repeat.
133133
// Play: https://go.dev/play/p/I3yndUQ-rhh
134134
func RandFloats(length int, min, max float64, precision int) []float64 {
135+
if max < min {
136+
min, max = max, min
137+
}
138+
139+
maxLength := int((max - min) * math.Pow10(precision))
140+
if maxLength == 0 {
141+
maxLength = 1
142+
}
143+
if length > maxLength {
144+
length = maxLength
145+
}
146+
135147
nums := make([]float64, length)
136148
used := make(map[float64]struct{}, length)
137149
for i := 0; i < length; {

random/random_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ func TestRandFloats(t *testing.T) {
197197
}
198198

199199
assert.Equal(len(numbers), 5)
200+
201+
numbers2 := RandFloats(10, 3.14, 3.2, 2)
202+
for _, n := range numbers2 {
203+
assert.GreaterOrEqual(n, 3.14)
204+
assert.Less(n, 3.2)
205+
}
206+
207+
assert.Equal(len(numbers2), 6)
200208
}
201209

202210
func TestRandIntSlice(t *testing.T) {

0 commit comments

Comments
 (0)