// Package robotname gives random names to robots.
package robotname
import (
"fmt"
"math/rand"
"time"
)
// Robot is a robot.
type Robot struct {
name string
init bool
}
const (
// len("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
alphabetSize = 26
// letters * letters * 10*10*10
possibility = alphabetSize * alphabetSize * 1000
)
// collects the already issued names
var issued = make(map[string]struct{})
// Name returns the name of the Robot. Generates a new name if Robot has no name yet.
// Returns error when no unique names left.
func (r *Robot) Name() (string, error) {
if r.init == true {
return r.name, nil
}
rand.Seed(time.Now().UnixNano())
for len(issued) < possibility {
r.name = fmt.Sprintf("%c%c%03d",
rand.Intn(alphabetSize)+'A', rand.Intn(alphabetSize)+'A',
rand.Intn(1000))
if _, ok := issued[r.name]; ok {
continue
}
issued[r.name] = struct{}{}
r.init = true
return r.name, nil
}
return "", fmt.Errorf("run out of possibilities")
}
// Reset resets the receiver.
func (r *Robot) Reset() {
r.init = false
}