random number in java, I need number every 30, I mean 30-60-90

I guess the votes here are for lack of research.

Anyway, you use java.util.Random‘s nextInt and multiply by 30. For instance, this gives you a random number 30 (1 * 30), 60 (2 * 3), 90 (3 * 30), and so on up to and including 3000 (100 * 30):

Random r = new Random();
int num = (r.nextInt(100) + 1) * 30;

Live Example

Remove the + 1 if 0 is a valid value to use (and then you won’t get 3000, the highest will be 2970).

Leave a Comment