Using the Math Class

AP Computer Science A· difficulty 4/5

int low = 5;
int high = 15;
int r = (int)(Math.random() * (high - low + 1)) + low;

Which range of integer values can r take?

  • A

    0 to 10 inclusive

  • B

    5 to 15 inclusive

    check_circle
  • C

    5 to 16 inclusive

  • D

    5 to 14 inclusive

Explanation

Math.random() * 11 yields [0.0, 11.0); cast to int gives 0-10; adding 5 gives 5-15 inclusive.

Want 10 more like this — adaptive to your weak spots?

Related questions

AP Computer Science A · Using the Math Class Practice Question | Acemy