From 2e215562f605dacd124f15eb89523bdc32da3a41 Mon Sep 17 00:00:00 2001 From: Mark Canning Date: Fri, 3 Apr 2015 15:07:17 -0700 Subject: [PATCH] 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). --- uniuri.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uniuri.go b/uniuri.go index 2063037..e256ea5 100644 --- a/uniuri.go +++ b/uniuri.go @@ -57,7 +57,7 @@ func NewLenChars(length int, chars []byte) string { if clen > 256 { panic("uniuri: maximum length of charset for NewLenChars is 256") } - maxrb := 256 - (256 % clen) + maxrb := 255 - (256 % clen) b := make([]byte, length) r := make([]byte, length+(length/4)) // storage for random bytes. i := 0