-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.go
More file actions
52 lines (40 loc) · 1.08 KB
/
Random.go
File metadata and controls
52 lines (40 loc) · 1.08 KB
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
package main
import (
"math/rand"
"time"
)
type Random interface {
Int(from int, to int) (randomInt int)
Duration(from time.Duration, to time.Duration) time.Duration
}
type RealRandom struct{}
func (instance *RealRandom) Int(min int, max int) (randomInt int) {
rand.Seed(time.Now().Unix())
return rand.Intn(max-min) + min
}
func (instance *RealRandom) Duration(min time.Duration, max time.Duration) time.Duration {
rand.Seed(time.Now().Unix())
return time.Duration(rand.Int63n(int64(max-min) + int64(min)))
}
func NewRealRandom() *RealRandom {
return &RealRandom{}
}
type FakeRandom struct {
number int
duration time.Duration
}
func (instance *FakeRandom) Int(from int, to int) (randomInt int) {
return instance.number
}
func (instance *FakeRandom) Duration(from time.Duration, to time.Duration) time.Duration {
return instance.duration
}
func (instance *FakeRandom) ForIntUse(value int) {
instance.number = value
}
func (instance *FakeRandom) ForDurationUse(value time.Duration) {
instance.duration = value
}
func NewFakeRandom() (random *FakeRandom) {
return &FakeRandom{0, 0}
}