mrand: fix bug in Hex with it not outputting correct number of characters for odd number inputs

This commit is contained in:
Brian Picciano 2018-08-13 19:40:15 -04:00
parent 8084268bbf
commit 526e35cf3f

View File

@ -25,8 +25,12 @@ func (r Rand) Bytes(n int) []byte {
// Hex returns a random hex string which is n characters long. // Hex returns a random hex string which is n characters long.
func (r Rand) Hex(n int) string { func (r Rand) Hex(n int) string {
origN := n
if n%2 == 1 {
n++
}
b := r.Bytes(hex.DecodedLen(n)) b := r.Bytes(hex.DecodedLen(n))
return hex.EncodeToString(b) return hex.EncodeToString(b)[:origN]
} }
// Element returns a random element from the given slice. // Element returns a random element from the given slice.