45

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Georgian Citizen
  • 3,727
  • 6
  • 38
  • 46

6 Answers6

63

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • thanks, note: i used getRandomDigits() in another groovy class and it works properly without any error. you can search about this method any see it's effect – Georgian Citizen Nov 22 '10 at 10:21
  • `getRandomDigits()` does not exist on java.util.Random as @mark-byers wrote. What class are you referring to? – youri Oct 06 '14 at 16:41
38

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.

anataliocs
  • 10,427
  • 6
  • 56
  • 72
  • 2
    Slight correction: Math.abs(new Random().nextInt() % 600) + 1. Otherwise you could get a mod result of -1 and an out of range result of 0 rather than the correct value of 2. – Chip McCormick Oct 13 '14 at 21:50
  • @anataliocs : If my requirement is to generate a random value in the range of 40 to 99, what should my approach be? – hirosht Sep 13 '18 at 07:40
  • 2
    @hirosht Math.abs(new Random().nextInt() % (UpperLimit - LowerLimit)) + LowerLimit – El Cas Jan 07 '19 at 18:20
  • 2
    Please refer to https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator before choosing this technique. Applying modulo arithmetic to a random value risks that the values are not generated with equal probability. – Mark Waite Aug 29 '19 at 10:09
  • @MarkWaite Any code running on commodity hardware is deterministic to a certain extent. If you were able to generate random numbers with a truly distributed probability with an equal chance to select any number in the range then that would be a True Random Number by definition which is not currently possible on commodity hardware as far as I know. https://simplicable.com/new/pseudorandom-vs-random Could you please expand on what you are saying? – anataliocs Sep 11 '19 at 21:49
  • @anataliocs the article I linked in my comment describes the problem much better than I can describe it. Modulo arithmetic to generate a subset will generally provide fewer of the largest value in the range than other values in the range unless the maximum random value is an even multiple of the desired subset range. See the preferred answer to https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator – Mark Waite Sep 13 '19 at 01:01
13

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

With Apache's RandomUtils you don't have to worry about storing the Random object for subsequent calls.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 1
    Can you give a code example supporting your statement? It's not immediately obvious to me why `apache.commons.lang.math.RandomUtils` would be easier to use than `java.util.Random` – jayhendren Sep 07 '18 at 20:46
3

If you want to generate random numbers in range including '0' , use the following while 'max' is the maximum number in the range.

Random rand = new Random()
random_num = rand.nextInt(max+1)
2

For example, let's say that you want to create a random number between 50 and 60, you can use one of the following methods.

new Random().nextInt()%6 +55

new Random().nextInt()%6 returns a value between -5 and 5. and when you add it to 55 you can get values between 50 and 60

Second method:

Math.abs(new Random().nextInt()%11) +50

Math.abs(new Random().nextInt()%11) creates a value between 0 and 10. Later you can add 50 which in the will give you a value between 50 and 60

Etibar - a tea bar
  • 1,912
  • 3
  • 17
  • 30
  • 2
    Please refer to https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator before choosing this technique. Applying modulo arithmetic to a random value risks that the values are not generated with equal probability. – Mark Waite Aug 29 '19 at 10:11
2

Easiest way: It will generate random number within 0 to (max_num-1)

new Random().nextInt(max_num)
Md. Shahariar Hossen
  • 1,367
  • 10
  • 11