Commit 7d1c5328 authored by Rob Pike's avatar Rob Pike

math/rand: Intn etc. should panic if their argument is <= 0.

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
parent 566e0fe7
...@@ -49,9 +49,10 @@ func (r *Rand) Int() int { ...@@ -49,9 +49,10 @@ func (r *Rand) Int() int {
} }
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n). // 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 { func (r *Rand) Int63n(n int64) int64 {
if n <= 0 { if n <= 0 {
return 0 panic("invalid argument to Int63n")
} }
max := int64((1 << 63) - 1 - (1<<63)%uint64(n)) max := int64((1 << 63) - 1 - (1<<63)%uint64(n))
v := r.Int63() v := r.Int63()
...@@ -62,9 +63,10 @@ func (r *Rand) Int63n(n int64) int64 { ...@@ -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). // 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 { func (r *Rand) Int31n(n int32) int32 {
if n <= 0 { if n <= 0 {
return 0 panic("invalid argument to Int31n")
} }
max := int32((1 << 31) - 1 - (1<<31)%uint32(n)) max := int32((1 << 31) - 1 - (1<<31)%uint32(n))
v := r.Int31() v := r.Int31()
...@@ -75,7 +77,11 @@ func (r *Rand) Int31n(n int32) int32 { ...@@ -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). // 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 { func (r *Rand) Intn(n int) int {
if n <= 0 {
panic("invalid argument to Intn")
}
if n <= 1<<31-1 { if n <= 1<<31-1 {
return int(r.Int31n(int32(n))) return int(r.Int31n(int32(n)))
} }
...@@ -125,12 +131,15 @@ func Int31() int32 { return globalRand.Int31() } ...@@ -125,12 +131,15 @@ func Int31() int32 { return globalRand.Int31() }
func Int() int { return globalRand.Int() } func Int() int { return globalRand.Int() }
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n). // 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) } func Int63n(n int64) int64 { return globalRand.Int63n(n) }
// Int31n returns, as an int32, a non-negative pseudo-random number in [0,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) } func Int31n(n int32) int32 { return globalRand.Int31n(n) }
// Intn returns, as an int, a non-negative pseudo-random number in [0,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) } func Intn(n int) int { return globalRand.Intn(n) }
// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0). // Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment