-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluky_integer.go
More file actions
32 lines (28 loc) · 784 Bytes
/
fluky_integer.go
File metadata and controls
32 lines (28 loc) · 784 Bytes
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
package fluky
type IntegerOptionsFn func(i *IntegerOptions)
type IntegerOptions struct {
min int
max int
}
// WithIntRange configure min and max values for integer random
func WithIntRange(min, max int) IntegerOptionsFn {
return func(i *IntegerOptions) {
i.min = min
i.max = max
}
}
// Integer random integer value from range [min, max]
// from −(2^62) to 2^62 − 1 by default (int64)
// to avoid overflow for int64 positive range
// using values out of this range doesn't warranty
// that result will be in range [min, max]
func (f *Fluky) Integer(opts ...IntegerOptionsFn) int {
o := &IntegerOptions{min: minInt63, max: maxInt63}
for _, optFn := range opts {
optFn(o)
}
if o.max == o.min {
return o.min
}
return int(f.rng.Int63())%(o.max-o.min+1) + o.min
}