Asad Rehman
Asad Rehman

Reputation: 84

random number generation in java with multiple of 1000 a number appear

I am looking to get random number in between 1000 to 8192000. The random number should be like 1000 , 2000,3000 to 8192000.

Following is the code that i have tried but did not got any success.

ran.nextInt(8192000 - 1000)%1000; 

What should I change in order to get number in term of 1000, 2000, 3000...

Upvotes: 1

Views: 1517

Answers (2)

Wojciech Kumoń
Wojciech Kumoń

Reputation: 325

If you want 8192000 inclusive try:

Random random = new Random();
for (int i = 0; i < 10; i++) {
  System.out.println((random.nextInt(8192) + 1) * 1000);
}

Here you get values: 1000, 2000, ..., 8192000

Upvotes: 2

Mureinik
Mureinik

Reputation: 311228

The easiest approach would seem to generate a random number between 1 and 8192 and just multiply it by 1000:

Random randomGenerator = new Random();
long randomNumber = (1 + randomGenerator.nextInt(8192)) * 1000L;

Upvotes: 4

Related Questions