Getting random numbers in java

Total Post:117

Points:821
Java 
 1320  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. 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!