Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

random numbers  RSS feed

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • 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
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!