github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/util/words/generator/generator.go (about)

     1  package generator
     2  
     3  import (
     4  	"math/rand"
     5  	"strings"
     6  	"time"
     7  
     8  	. "code.cloudfoundry.org/cli/util/words"
     9  )
    10  
    11  //go:generate counterfeiter . WordGenerator
    12  
    13  type WordGenerator interface {
    14  	Babble() string
    15  }
    16  
    17  type wordGenerator struct {
    18  	numberGenerator *rand.Rand
    19  	adjectives      []string
    20  	nouns           []string
    21  }
    22  
    23  func (wg wordGenerator) Babble() (word string) {
    24  	idx := int(wg.numberGenerator.Int()) % len(wg.adjectives)
    25  	word = wg.adjectives[idx] + "-"
    26  	idx = int(wg.numberGenerator.Int()) % len(wg.nouns)
    27  	word += wg.nouns[idx]
    28  	return
    29  }
    30  
    31  func NewWordGenerator() WordGenerator {
    32  	adjectiveBytes, _ := Asset("util/words/dict/adjectives.txt")
    33  	nounBytes, _ := Asset("util/words/dict/nouns.txt")
    34  	source := rand.NewSource(time.Now().UnixNano())
    35  
    36  	return wordGenerator{
    37  		adjectives:      strings.Split(string(adjectiveBytes), "\n"),
    38  		nouns:           strings.Split(string(nounBytes), "\n"),
    39  		numberGenerator: rand.New(source),
    40  	}
    41  }