1

I thought

Math.random() * (max-min) // 1

would be shorter and comfortable than

Math.floor Math.random() * (max-min)

But I'm not sure whether the former is safe or not.

2 Answers 2

1

I didn't know about the // operator but if we take a look at the JavaScript output of both versions we see that they are equivalent.

First: Math.floor(Math.random() * (max - min) / 1);

Second: Math.floor(Math.random() * (max - min));

(Dividing a number by 1 in JavaScript has no effect)

Sign up to request clarification or add additional context in comments.

Comments

0

That usage is explicitly supported by the specs. So it is quite safe to use // as you intend. To quote the doc:

To simplify math expressions [...] // performs integer division

And latter:

CoffeeScript        JavaScript
-------------------------------------

a // b              Math.floor(a / b)

Please note this operator was added in CoffeScript 1.7.0

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.