Skip to content

Commit

Permalink
Cache modulo operator in New
Browse files Browse the repository at this point in the history
Instead of recreating the operator in each iterator, we can cache it before the loop.
  • Loading branch information
mrnugget committed Aug 6, 2014
1 parent 5b429c8 commit d0f5a8f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pwgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-?
// New() generates a random string of the given length
func New(length int) string {
var bytes = make([]byte, length)
var op = byte(len(chars))

rand.Read(bytes)
for i, b := range bytes {
bytes[i] = chars[b%byte(len(chars))]
bytes[i] = chars[b%op]
}
return string(bytes)
}

0 comments on commit d0f5a8f

Please sign in to comment.