-2

I am wondering how to generate a number in interval of [0,360) which would be also divisible by number 15. Examples: 0, 15, 30, 45, 50.. I can generate a number in the interval with:

    (int)(Math.random()*360));

But I don't know how to make sure that the generated number is divisible by number 15.

Jack
  • 139
  • 8
  • 2
    Given that your intended integer is `n`, consider the range that `n/15` will fall into. This should then lead to a clear next step. – nanofarad Nov 01 '18 at 20:21

2 Answers2

2

Welcome to SO Michal!

One easy way to accomplish this is to generate a random number less than 24 (=360/15), then multiply the result by 15:

(int)(Math.random()*24)) * 15;
nvioli
  • 4,137
  • 3
  • 22
  • 38
2

Taking some information from this previous question, you can just generate a random number between the range 0 to 24 and multiply it by 15.

AbsoluteSpace
  • 710
  • 2
  • 11
  • 21