how can i use random numbers in groovy?

前端 未结 5 1012
野性不改
野性不改 2020-12-13 17:49

I use this method:

def getRandomNumber(int num){
    Random random = new Random()
    return random.getRandomDigits(num)
}

when I call it I

5条回答
  •  [愿得一人]
    2020-12-13 18:32

    There is no such method as java.util.Random.getRandomDigits.

    To get a random number use nextInt:

    return random.nextInt(10 ** num)
    

    Also you should create the random object once when your application starts:

    Random random = new Random()
    

    You should not create a new random object every time you want a new random number. Doing this destroys the randomness.

提交回复
热议问题