random accepts generator

master
Bel LaPointe 2023-03-02 09:31:40 -07:00
parent 7202fef15d
commit fe16e2325c
3 changed files with 13 additions and 8 deletions

View File

@ -5,5 +5,5 @@ type Input interface {
}
func New() Input {
return NewRandom('a', 'g')
return NewRandom(RandomCharFromRange('a', 'g'))
}

View File

@ -11,14 +11,13 @@ type Button struct {
}
type Random struct {
start byte
stop byte
down []Button
generator func() byte
down []Button
}
func NewRandom(start, stop byte) *Random {
func NewRandom(generator func() byte) *Random {
rand.Seed(time.Now().UnixNano())
return &Random{start: start, stop: stop}
return &Random{generator: generator}
}
func (r *Random) Read() []Button {
@ -30,8 +29,14 @@ func (r *Random) Read() []Button {
r.down = r.down[:0]
return was
} 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)
return []Button{c}
}
}
func RandomCharFromRange(start, stop byte) func() byte {
return func() byte {
return start + byte(rand.Int()%int(1+stop-start))
}
}

View File

@ -6,7 +6,7 @@ import (
)
func TestRandom(t *testing.T) {
r := input.NewRandom('a', 'g')
r := input.NewRandom(input.RandomCharFromRange('a', 'z'))
for i := 0; i < 100; i++ {
batch := r.Read()
for _, got := range batch {