GETTING RANDOM NUMBERS IN JAVA

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
Java 
 1113  View(s)
Ratings:
Rate this:
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
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Getting random numbers in java

    Hi Jayprakash!


    Try as following

    Random rand = new Random();

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!