-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint32.go
43 lines (34 loc) · 884 Bytes
/
int32.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
package chance
import "math"
// Int32Option is a type
type Int32Option func(*Int32Options)
// Int32Options is int32 options
type Int32Options struct {
min int32
max int32
}
func (ch *chance) Int32(options ...Int32Option) int32 {
ops := Int32Options{min: -math.MaxInt32, max: math.MaxInt32}
for i := range options {
options[i](&ops)
}
ch.r.Seed(ch.seed)
// TODO: handle error on bad options
return int32(ch.r.Int63n(int64(ops.max)-int64(ops.min)) + int64(ops.min))
}
// Int32 returns a random int32
func Int32(options ...Int32Option) int32 {
return defaultChance.Int32(options...)
}
// SetInt32Min sets min of random int32
func SetInt32Min(min int32) Int32Option {
return func(iOpts *Int32Options) {
iOpts.min = min
}
}
// SetInt32Max sets max of random int32
func SetInt32Max(max int32) Int32Option {
return func(iOpts *Int32Options) {
iOpts.max = max
}
}