Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# random numbers

Ranch Hand
Posts: 802
ok, i need to generate a random number, but only between

40 and 240, and only muliples of 20..

40,60,80,...,240;

can anyone help?

Justin

Ranch Hand
Posts: 94
you can't do directly from what is available in the API, but with a little thought and arithmetic you might come up with a way to do this. I am curious what problem solving steps you attempted, since this is an easy problem. Problem solving is very important, the API can't do all your thinking for you.

If all you needed to do was get a number between 40 and 240 that is easily accomplished with the nextInt(int n) method and simple addition.

What you need is actually simpler. If you multiply 2*20 you get 40, if you multiply 12*20 you get 240. Since you are multiplying by 20, they are obviously multiples of 20. So now you know the range of the random numbers, and the multiple. Should be a piece of cake.

Sorry for basically giving you the solution. It is hard to give hints for these simple problems without solving it.
[ September 30, 2006: Message edited by: Robert Hill ]

Justin Fox
Ranch Hand
Posts: 802
yeah, I figured it out...

i just had a method isACoord(int a)..

and returned true if it was > than 40, and a % 20 = 0;

Justin