Q:

how can i use random numbers in groovy? 

I use this method:
def getRandomNumber(int num){
    Random random = new Random()
    return random.getRandomDigits(num)
}

when I call it I write println getRandomNumber(4)
but I have an error
No signature of method: java.util. Random.getRandomDigits() is applicable for argument types: (java.lang. Integer) values: [4]

Note: I use this method in another groovy class and it works properly without any error 

A:

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. 

A:

Generate pseudo random numbers between 1 and an [UPPER_LIMIT]
You can use the following to generate a number between 1 and an upper limit. 
Math.abs(new Random().nextInt() % [UPPER_LIMIT]) + 1
Here is a specific example:
Example - Generate pseudo random numbers in the range 1 to 600:
Math.abs(new Random().nextInt() % 600) + 1

This will generate a random number within a range for you.   In this case 1-600.   You can change the value 600 to anything you need in the range of integers. 

Generate pseudo random numbers between a [LOWER_LIMIT] and an [UPPER_LIMIT]
If you want to use a lower bound that is not equal to 1 then you can use the following formula. 
Math.abs(new Random().nextInt() % ([UPPER_LIMIT] - [LOWER_LIMIT])) + [LOWER_LIMIT]
Here is a specific example:
Example - Generate pseudo random numbers in the range of 40 to 99:
Math.abs( new Random().nextInt() % (99 - 40) ) + 40

This will generate a random number within a range of 40 and 99. 

A:

Generally, I find RandomUtils (from Apache commons lang) an easier way to generate random numbers than java.util. Random

