Skip to content

Commit be7f344

Browse files
committed
minor improvements to RNG sizing and docs
1 parent 38ece4f commit be7f344

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

rng.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func (r RNG) Uint16() uint16 {
3131
return binary.LittleEndian.Uint16(p[:])
3232
}
3333

34-
// Uintn generates a random uint in the interval [0, max).
34+
// Uintn generates a random uint in the interval [0, max). It panics if
35+
// max == 0.
3536
func (r RNG) Uintn(max uint) uint {
3637
bad := ^uint(0) - ^uint(0)%max
3738
x := uint(r.Uint64())
@@ -41,7 +42,7 @@ func (r RNG) Uintn(max uint) uint {
4142
return x % max
4243
}
4344

44-
// Intn generates a random int in the interval [0, max). It panics if max < 0.
45+
// Intn generates a random int in the interval [0, max). It panics if max <= 0.
4546
func (r RNG) Intn(max int) int {
4647
if max < 0 {
4748
panic("maximum below zero")
@@ -51,7 +52,7 @@ func (r RNG) Intn(max int) int {
5152

5253
// Big generates a random number with maximum bit length nbits.
5354
func (r RNG) Big(nbits int) *big.Int {
54-
p := make([]byte, uint(nbits)>>3+uint(nbits|nbits>>1|nbits>>2)&1)
55+
p := make([]byte, (uint(nbits)+7)>>3)
5556
r.Read(p)
5657
p[0] &= byte(0xff >> (8 - uint(nbits)&7))
5758
return new(big.Int).SetBytes(p)

0 commit comments

Comments
 (0)