-
Notifications
You must be signed in to change notification settings - Fork 0
/
uint8.go
43 lines (34 loc) · 871 Bytes
/
uint8.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"
// UInt8Option is a type
type UInt8Option func(*UInt8Options)
// UInt8Options is uint8 options
type UInt8Options struct {
min uint8
max uint8
}
func (ch *chance) UInt8(options ...UInt8Option) uint8 {
ops := UInt8Options{min: 0, max: math.MaxUint8}
for i := range options {
options[i](&ops)
}
ch.r.Seed(ch.seed)
// TODO: handle error on bad options
return uint8(ch.r.Int31n(int32(ops.max)-int32(ops.min)) + int32(ops.min))
}
// UInt8 returns a random uint8
func UInt8(options ...UInt8Option) uint8 {
return defaultChance.UInt8(options...)
}
// SetUInt8Min sets min of random uint8
func SetUInt8Min(min uint8) UInt8Option {
return func(iOpts *UInt8Options) {
iOpts.min = min
}
}
// SetUInt8Max sets max of random uint8
func SetUInt8Max(max uint8) UInt8Option {
return func(iOpts *UInt8Options) {
iOpts.max = max
}
}