undefined (type *math/rand.Rand has no field or method randIntn)

72 views
Skip to first unread message

Anjana Prabhakar

unread,
Feb 25, 2020, 1:42:31 PM2/25/20
to golang-nuts
Hi All,

I am trying to generate a random integer using the below code snippet:
package ABC
import (
*****
"math/rand"
)
func execBlock(
logger log.Logger,) {
var validTxs, invalidTxs, unchkdTxs = 0, 0, 0

txIndex := 0
proxyCb := func(req *xxx, res *yyy) {
if r, ok := res.Value.(*yyyy); ok {
txRes := r.DeliverTx
// code for random integer begin
  s1 := rand.NewSource(time.Now().UnixNano())
    r1 := rand.New(s1)
toCheck := r1.randIntn(100) // getting error for this line
if toCheck <= 65{
***********
}else {
*********
}// end
}
}
}

I get the error as r1.randIntn undefined (type *math/rand.Rand has no field or method randIntn). Please help.

Thanks,
Anjana

burak serdar

unread,
Feb 25, 2020, 1:46:29 PM2/25/20
to Anjana Prabhakar, golang-nuts
It doesn't have randIntn, it has Intn, so use r1.Intn(100)

>
> Thanks,
> Anjana
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5f0fc1e1-3554-4d2d-b7e4-0fbc196e1b53%40googlegroups.com.

Jake Montgomery

unread,
Feb 25, 2020, 2:33:35 PM2/25/20
to golang-nuts

On Tuesday, February 25, 2020 at 1:42:31 PM UTC-5, Anjana Prabhakar wrote:
I get the error as r1.randIntn undefined (type *math/rand.Rand has no field or method randIntn). Please help.

The message is  pretty clear. The first step I would take with a message like that is to look at the documentation for the type that the compiler has so kindly provided. If you go to the documentation for the rand package, you will see that the Rand struct has no method 'randIntn', just as the message says. The next step would be to read about the methods that are available for rand.Rand, and see if any of them fit your needs.

(The fact that randIntn starts with a lower case letter is a big hint that there could be no such public method.)

Good Luck

Anjana Prabhakar

unread,
Feb 26, 2020, 1:42:58 AM2/26/20
to golang-nuts
Thank you.
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

Anjana Prabhakar

unread,
Feb 26, 2020, 1:43:16 AM2/26/20
to golang-nuts
Thank you.
Reply all
Reply to author
Forward
0 new messages