-
Notifications
You must be signed in to change notification settings - Fork 0
/
chance.go
68 lines (57 loc) · 1.39 KB
/
chance.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
// Package chance is a random generator in Go
package chance
import "math/rand"
// Chance is interface of chance
type Chance interface {
Bool(options ...BoolOption) bool
String(options ...StringOption) string
Int(options ...IntOption) int
Int8(options ...Int8Option) int8
Int16(options ...Int16Option) int16
Int32(options ...Int32Option) int32
Int64(options ...Int64Option) int64
UInt(options ...UIntOption) uint
UInt8(options ...UInt8Option) uint8
UInt16(options ...UInt16Option) uint16
UInt32(options ...UInt32Option) uint32
Byte(options ...ByteOption) byte
Rune(options ...RuneOption) rune
UInt64(options ...UInt64Option) uint64
Float32(options ...Float32Option) float32
}
type chance struct {
r *rand.Rand
seed int64
}
// Option is a type
type Option func(Chance)
var defaultChance Chance
func init() {
defaultChance = New()
}
// New returns new instance of chance
// chance.New(chance.SetSeed(time.Now().UnixNano()))
func New(options ...Option) Chance {
ch := new(chance)
// defaults
ResetSeed()(ch)
ch.r = rand.New(rand.NewSource(ch.seed))
for i := range options {
options[i](ch)
}
return ch
}
// SetSeed sets seed option of instance
func SetSeed(seed int64) Option {
return func(ich Chance) {
ch := ich.(*chance)
ch.seed = seed
}
}
// ResetSeed resets seed option of instance
func ResetSeed() Option {
return func(ich Chance) {
ch := ich.(*chance)
ch.seed = 1
}
}