Advertise with us

Getting random numbers in java

Hello Guys,

I would like to get a random value between 1 to 50.

How may I do that with the help of Math.random(); in Java?

Is there any way to bound the values that will be accepted?

Thank you
Last updated:5/7/2013 7:50:19 AM

1 Answers

AVADHESH PATEL
AVADHESH PATEL

Hi Jayprakash!


Try as following

Random rand = new Random();

int  n = rand.nextInt(50) + 1;

Answer