Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a random number in java between 450 and 150 that is a multiple of 10?

Tags:

java

random

So far I have this code

int w = (int)((450-150)*random()+150);  

This generates a number between 450 and 150... But I have no idea how to make that number a multiple of 10.

like image 394
Cheesegraterr Avatar asked Oct 18 '10 18:10

Cheesegraterr


People also ask

How do you generate multiple random numbers in Java?

If you want to create random numbers in the range of integers in Java than best is to use random. nextInt() method it will return all integers with equal probability. You can also use Math. random() method to first create random number as double and than scale that number into int later.

How do you generate a random number between 1 and 10 inclusive in Java?

For example, to generate a random number between 1 and 10, we can do it like below. ThreadLocalRandom random = ThreadLocalRandom. current(); int rand = random. nextInt(1, 11);


2 Answers

Easy. Just generate random numbers between 15 and 45...then multiply by 10:

int w = ((int)((45 - 15) * random() + 15)) * 10;
like image 166
Justin Niessner Avatar answered Oct 29 '22 22:10

Justin Niessner


Pick a random number between 45 and 15 and multiply it with 10 -

int w = (int)((45-15)*random()+15) * 10; 
like image 20
Gaurav Saxena Avatar answered Oct 29 '22 22:10

Gaurav Saxena