-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric.go
72 lines (57 loc) · 1.21 KB
/
numeric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package gen
// Numeric represents numeric types constraint.
type Numeric interface {
uint8 | uint16 | uint32 | uint64 | uint | int8 | int16 | int32 | int64 | int | float32 | float64
}
func numericMin[T Numeric](a, b T) T {
if a < b {
return a
}
return b
}
func numericMax[T Numeric](a, b T) T {
if a > b {
return a
}
return b
}
func randUint8(n uint8) uint8 {
return uint8(random.Int31n(int32(n)))
}
func randUint16(n uint16) uint16 {
return uint16(random.Int31n(int32(n)))
}
func randUint32(n uint32) uint32 {
return uint32(random.Int31n(int32(n)))
}
func randUint64(n uint64) uint64 {
return uint64(random.Int63n(int64(n)))
}
func randUint(n uint) uint {
if n <= 1<<32-1 {
return uint(randUint32(uint32(n)))
} else {
return uint(randUint64(uint64(n)))
}
}
func randInt8(n int8) int8 {
return int8(random.Int31n(int32(n)))
}
func randInt16(n int16) int16 {
return int16(random.Int31n(int32(n)))
}
func randInt32(n int32) int32 {
return random.Int31n(n)
}
func randInt64(n int64) int64 {
return random.Int63n(n)
}
func randInt(n int) int {
return random.Intn(n)
}
func randFloat32(n float32) float32 {
return random.Float32() * n
}
func randFloat64(n float64) float64 {
return random.Float64() * n
}