Skip to content

math/rand panic:runtime error: index out of range #16205

@rjcb99

Description

@rjcb99

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6.1 darwin/amd64
  2. What operating system and processor architecture are you using (go env)?
    ubuntu server 12.04
  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    A link on play.golang.org is best.
    ps: this func run too many times by many goroutine and just panic once.
    // Random [m,n]
    func GetRandom(m, n int) int {
    if randnum == nil {
    randnum = rand.New(rand.NewSource(time.Now().UnixNano()))
    }
    if m > n {
    return 0
    }
    if m == n {
    return m
    }
    return m + randnum.Intn(n-m+1)
    }
  4. What did you expect to see?
    return a expect rand int32
  5. What did you see instead?
    panic: runtime error: index out of range

goroutine 1018903 [running]:
panic(0x941da0, 0xc8200100a0)
/usr/local/go/src/runtime/panic.go:464 +0x3e6
math/rand.(_rngSource).Int63(0xc82008d500, 0x0)
/usr/local/go/src/math/rand/rng.go:244 +0xc1
math/rand.(_Rand).Int63(0xc8200e5740, 0x0)
/usr/local/go/src/math/rand/rand.go:46 +0x39
math/rand.(_Rand).Int31(0xc8200e5740, 0xc82469dec8)
/usr/local/go/src/math/rand/rand.go:52 +0x21
math/rand.(_Rand).Int31n(0xc8200e5740, 0x5, 0xc82469dec0)
/usr/local/go/src/math/rand/rand.go:87 +0xc1
math/rand.(*Rand).Intn(0xc8200e5740, 0x5, 0x0)
/usr/local/go/src/math/rand/rand.go:101 +0x92
utils.GetRandom(0x3, 0x7, 0x1)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions