-1

So I'm trying to figure out how to print out a random number between 1 to 1000.

I tried:

 double a = 1+ (Math.random()*1000);
            System.out.println(a);

But when I try this i get numbers with a bunch of decimals. I do not want any decimals. Anyone can help? I want to get a value like 50 or 289 or 294. I do not want to get a number like 234.5670242 or 394.220345. Help if you can. will appreciate it. Thank you.

1 Answers1

0

Try this:

 public static void main(String args[])
 {
     // define the range
     int max = 1000;
     int min = 1;
     int range = max - min + 1;

     // generate random numbers within 1 to 1000
     for (int i = 0; i < 1000; i++) {
         int rand = (int)(Math.random() * range) + min;
  
         // Output
         System.out.println(rand);
     }
 }
Roberto Rivera
  • 255
  • 2
  • 9