What does Math.floor(Math.random() ); combination do.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What does Math.floor(Math.random() ); combination do..

I have seen this a lot of times here .. and I have tried combining Math.Ceil(Math.random() ) Math.round(Math.random() ) Math.atan(math.abs() ) etc I don't even know if this is right. .I tried all the Maths() functions and none of them works except the Math.floor(Math.random() ) can anyone try explaining all these maths combining mybe even the rest.. THANK YOU..

4th Jan 2018, 5:31 AM
_c0d£ Bit£_
_c0d£ Bit£_ - avatar
2 Answers
+ 7
Math.floor(Math.random()) will always return 0 as Math.random is a randomly chosen number between 0 and 1, and you're asking for the greatest integer <= your number using Math.floor. For the same reason, Math.ceil(Math.random()) will always return 1 because Math.ceil gives you the lowest integer >= your number. However, Math.round(Math.random()) will give you 0 half of the time, and 1 the rest.
4th Jan 2018, 5:48 AM
Dapper Mink
Dapper Mink - avatar
+ 5
Math.random() generates a random number between 0 and 1 while Math.floor reduces it to the nearest whole number.In this case 0 will always be your output
4th Jan 2018, 6:50 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar