-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
random.go
50 lines (42 loc) · 854 Bytes
/
random.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
package op
import (
"fmt"
"math/rand"
"time"
"github.com/emicklei/melrose/core"
)
type RandomInteger struct {
From core.HasValue
To core.HasValue
rnd *rand.Rand
last int
}
func NewRandomInteger(from, to core.HasValue) *RandomInteger {
rnd := &RandomInteger{
From: from,
To: to,
rnd: rand.New(rand.NewSource(time.Now().Unix())),
}
rnd.Next()
return rnd
}
// Storex is part of Storable
func (r RandomInteger) Storex() string {
return fmt.Sprintf("random(%s,%s)", core.Storex(r.From), core.Storex(r.To))
}
// Value is part of HasValue
func (r *RandomInteger) Value() interface{} {
return r.last
}
// Next is part of Nextable
func (r *RandomInteger) Next() interface{} {
f := core.Int(r.From)
t := core.Int(r.To)
if t < f {
r.last = f
return f
}
r.last = f + r.rnd.Intn(t-f+1)
return r.last
}
// TODO Replaceable