• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

random numbers

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic