]> Cypherpunks repositories - gostls13.git/commitdiff
math/rand: Intn etc. should panic if their argument is <= 0.
authorRob Pike <r@golang.org>
Fri, 17 Feb 2012 21:53:03 +0000 (08:53 +1100)
committerRob Pike <r@golang.org>
Fri, 17 Feb 2012 21:53:03 +0000 (08:53 +1100)
I am making a unilateral decision here. I could also settle for returning 0,
as long it's documented, but I argue that it's equivalent to an index
out of bounds.

Fixes #2892.

R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/5676079

src/pkg/math/rand/rand.go

index 8955219203075efc02465776f8548d2a3bd82f60..94f84a85fbe14a0f2e1f8d62a6ca1d3fc546dc93 100644 (file)
@@ -49,9 +49,10 @@ func (r *Rand) Int() int {
 }
 
 // Int63n returns, as an int64, a non-negative pseudo-random number in [0,n).
+// It panics if n <= 0.
 func (r *Rand) Int63n(n int64) int64 {
        if n <= 0 {
-               return 0
+               panic("invalid argument to Int63n")
        }
        max := int64((1 << 63) - 1 - (1<<63)%uint64(n))
        v := r.Int63()
@@ -62,9 +63,10 @@ func (r *Rand) Int63n(n int64) int64 {
 }
 
 // Int31n returns, as an int32, a non-negative pseudo-random number in [0,n).
+// It panics if n <= 0.
 func (r *Rand) Int31n(n int32) int32 {
        if n <= 0 {
-               return 0
+               panic("invalid argument to Int31n")
        }
        max := int32((1 << 31) - 1 - (1<<31)%uint32(n))
        v := r.Int31()
@@ -75,7 +77,11 @@ func (r *Rand) Int31n(n int32) int32 {
 }
 
 // Intn returns, as an int, a non-negative pseudo-random number in [0,n).
+// It panics if n <= 0.
 func (r *Rand) Intn(n int) int {
+       if n <= 0 {
+               panic("invalid argument to Intn")
+       }
        if n <= 1<<31-1 {
                return int(r.Int31n(int32(n)))
        }
@@ -125,12 +131,15 @@ func Int31() int32 { return globalRand.Int31() }
 func Int() int { return globalRand.Int() }
 
 // Int63n returns, as an int64, a non-negative pseudo-random number in [0,n).
+// It panics if n <= 0.
 func Int63n(n int64) int64 { return globalRand.Int63n(n) }
 
 // Int31n returns, as an int32, a non-negative pseudo-random number in [0,n).
+// It panics if n <= 0.
 func Int31n(n int32) int32 { return globalRand.Int31n(n) }
 
 // Intn returns, as an int, a non-negative pseudo-random number in [0,n).
+// It panics if n <= 0.
 func Intn(n int) int { return globalRand.Intn(n) }
 
 // Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).