github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/words/generator/generator.go (about)

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