random accepts generator
parent
7202fef15d
commit
fe16e2325c
|
|
@ -5,5 +5,5 @@ type Input interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() Input {
|
func New() Input {
|
||||||
return NewRandom('a', 'g')
|
return NewRandom(RandomCharFromRange('a', 'g'))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,14 +11,13 @@ type Button struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Random struct {
|
type Random struct {
|
||||||
start byte
|
generator func() byte
|
||||||
stop byte
|
|
||||||
down []Button
|
down []Button
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRandom(start, stop byte) *Random {
|
func NewRandom(generator func() byte) *Random {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
return &Random{start: start, stop: stop}
|
return &Random{generator: generator}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Random) Read() []Button {
|
func (r *Random) Read() []Button {
|
||||||
|
|
@ -30,8 +29,14 @@ func (r *Random) Read() []Button {
|
||||||
r.down = r.down[:0]
|
r.down = r.down[:0]
|
||||||
return was
|
return was
|
||||||
} else {
|
} else {
|
||||||
c := Button{Char: r.start + byte(rand.Int()%int(1+r.stop-r.start)), Down: true}
|
c := Button{Char: r.generator(), Down: true}
|
||||||
r.down = append(r.down, c)
|
r.down = append(r.down, c)
|
||||||
return []Button{c}
|
return []Button{c}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RandomCharFromRange(start, stop byte) func() byte {
|
||||||
|
return func() byte {
|
||||||
|
return start + byte(rand.Int()%int(1+stop-start))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRandom(t *testing.T) {
|
func TestRandom(t *testing.T) {
|
||||||
r := input.NewRandom('a', 'g')
|
r := input.NewRandom(input.RandomCharFromRange('a', 'z'))
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
batch := r.Read()
|
batch := r.Read()
|
||||||
for _, got := range batch {
|
for _, got := range batch {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue