Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions babble.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package babble

import (
"math/rand"
"crypto/rand"
"encoding/binary"
"strings"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

type Babbler struct {
Count int
Separator string
Rand func() uint32
Words []string
}

Expand All @@ -24,10 +21,25 @@ func NewBabbler() (b Babbler) {
}

func (this Babbler) Babble() string {
pieces := []string{}
for i := 0; i < this.Count ; i++ {
pieces = append(pieces, this.Words[rand.Int()%len(this.Words)])
pieces := make([]string, this.Count)
for idx := range this.Count {
rand := int(this.rand())
pieces[idx] = this.Words[rand%len(this.Words)]
}

return strings.Join(pieces, this.Separator)
}

func (this Babbler) rand() uint32 {
if this.Rand != nil {
return this.Rand()
}

var data [4]byte
_, err := rand.Read(data[:])
if err != nil {
panic(err)
}

return binary.LittleEndian.Uint32(data[:])
}
4 changes: 2 additions & 2 deletions babble_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package babble

import (
"io/ioutil"
"io"
"os"
"strings"
)
Expand All @@ -14,7 +14,7 @@ func readAvailableDictionary() (words []string) {
panic(err)
}

bytes, err := ioutil.ReadAll(file)
bytes, err := io.ReadAll(file)
if err != nil {
panic(err)
}
Expand Down