Correct for bias towards first character in chars

Currently the first character in chars has a 1/clen + 1/(maxrb+1) chance of being selected. In the case of StdChars the first character is ~25% more likely to be picked than any other character. This change gives a 1/clen chance to all characters, and is based on math/rand's Int31n(n).
This commit is contained in:
Mark Canning 2015-04-03 15:07:17 -07:00
parent 87c54b9ba5
commit 2e215562f6

View File

@ -57,7 +57,7 @@ func NewLenChars(length int, chars []byte) string {
if clen > 256 { if clen > 256 {
panic("uniuri: maximum length of charset for NewLenChars is 256") panic("uniuri: maximum length of charset for NewLenChars is 256")
} }
maxrb := 256 - (256 % clen) maxrb := 255 - (256 % clen)
b := make([]byte, length) b := make([]byte, length)
r := make([]byte, length+(length/4)) // storage for random bytes. r := make([]byte, length+(length/4)) // storage for random bytes.
i := 0 i := 0