Math.floor vs Math.ceil


#1

Is there a reason to use Math.floor instead of Math.ceil

var damageThisRound = Math.floor(Math.random()*5+1);

vs.

var damageThisRound = Math.ceil(Math.random()*5);

It seems like this would be the simpler. Am I missing something?


#2

Analyse:

  1. Let n = Math.random()*5. So, range for n is 0 <= n < 5.
  2. We have expressions A) Math.floor(n+1) and B) Math.ceil(n)
  3. At any decimal value, say 4.3, both A and B give same value - PASS!
  4. At any decimal value, say 4.7, both A and B give same value - PASS!
  5. At any decimal value, say 4.5, both A and B give same value - PASS!
  6. At any decimal value, say 4.0, both A and B give different value - ERROR!

Thus, both do not produce the same value for all values of n, and hence it would not be advisable to blindly replace all occurrences of expression A with B.

In case this is exercise related, I cannot help much without knowing the exercise link first.