From 28e216136a5e6d5a334a2fcd29b805e9906b2e92 Mon Sep 17 00:00:00 2001 From: Oleg Ivanov Date: Sun, 10 Aug 2014 00:37:31 +1000 Subject: [PATCH] fixed the same value always generated for 256-divisor chars --- uniuri.go | 2 +- uniuri_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/uniuri.go b/uniuri.go index 0e9ee1b..dad8559 100644 --- a/uniuri.go +++ b/uniuri.go @@ -56,7 +56,7 @@ func NewLenChars(length int, chars []byte) string { panic("error reading from random source: " + err.Error()) } for _, c := range r { - if c > maxrb { + if maxrb > 0 && c > maxrb { // Skip this number to avoid modulo bias. continue } diff --git a/uniuri_test.go b/uniuri_test.go index ff9a83d..b242e29 100644 --- a/uniuri_test.go +++ b/uniuri_test.go @@ -50,4 +50,9 @@ func TestNewLenChars(t *testing.T) { } // Check that only allowed characters are present validateChars(t, u, chars) + // Check that two generated strings are different + u2 := NewLenChars(length, chars) + if u == u2 { + t.Fatalf("not unique: %q and %q", u, u2) + } }