-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
55 lines (45 loc) · 1.04 KB
/
int.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
package chance
import (
"math"
"strconv"
)
// IntOption is a type
type IntOption func(*IntOptions)
// IntOptions is int options
type IntOptions struct {
min int
max int
}
func (ch *chance) Int(options ...IntOption) int {
ops := IntOptions{}
// Check machine architecture
if strconv.IntSize == 32 {
ops.min = -math.MaxInt32 / 2 // FIXME: Use full range
ops.max = math.MaxInt32 / 2 // FIXME: Use full range
} else {
ops.min = -math.MaxInt64 / 2 // FIXME: Use full range
ops.max = math.MaxInt64 / 2 // FIXME: Use full range
}
for i := range options {
options[i](&ops)
}
ch.r.Seed(ch.seed)
// TODO: handle error on bad options
return ch.r.Intn(ops.max-ops.min) + ops.min
}
// Int returns a random int
func Int(options ...IntOption) int {
return defaultChance.Int(options...)
}
// SetIntMin sets min of random int
func SetIntMin(min int) IntOption {
return func(iOpts *IntOptions) {
iOpts.min = min
}
}
// SetIntMax sets max of random int
func SetIntMax(max int) IntOption {
return func(iOpts *IntOptions) {
iOpts.max = max
}
}