+ 1

Whats the output

a = 9; b = 20; temp = a / b * 2 % 2 + Math.floor(Math.random()*5);

5th Oct 2017, 1:36 AM
Styxincewa
Styxincewa - avatar
3 Answers
+ 2
You should include a language tag. Is this Java or Javascript? They both have a Math.random() and a Math.floor(). Fortunately they both behave in a similar fashion. Math.random() will return a floating point number from 0 to 1 (exclusive). Meaning the highest number returned will be 0.99999. If you multiply .99999 * 5 you get 4.9999 then you take the Math.floor() of this number which will cut everything after the decimal point off. This means that you will get a pseudo random integer from 0 to 4. * is used for multiplication / is used for division (integer division in Java) % is used to get the modulus (division remainder) (example 9/4 = 2 with a remainder of 1 so the modulus is 1) (When the numerator is smaller than the denominator then the modulus is the numerator I.E. 5 % 7 = 5 a = 9; b = 20; temp = a / b * 2 % 2 + Math.floor(Math.random()*5); a/b is 9/20 = 0.45 in JavaScript or 0 in Java (This is why it is important to tag the programming language) (from here on down I'm going to assume JavaScript unless you specify otherwise) 0.45 * 2 = 0.9 0.9 % 2 = 0.9 Which leaves you with a number that is equal to 0.9 + a random integer from 0 to 4, or a number from 0.9 to 4.9. Possible answers are 0.9, 1.9, 2.9, 3.9 and 4.9.
5th Oct 2017, 4:04 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
JavaScript
5th Oct 2017, 7:27 PM
Styxincewa
Styxincewa - avatar
+ 1
The output will be ranging from 0..5
5th Oct 2017, 2:48 AM
RZK 022
RZK 022 - avatar