Math.random() misconception


#1

I don't know exactly how this work "Math.random()":
- input a value between 0 and 1 like 0.00032 or 0.99923
or
- input a value between 0 and 1 including 0.000 or 1.000
?
And this var youHit = Math.floor(Math.random() * 2); will work something like this:
if Math.random return 0.99999999999999999999999 Math.floor..blabla..*2 will return for youHit 1?
Oh.. and what's the difference between return and console.log()?


#2

@neagumihai54321gmail,

Math.random() return's a number Value in the range of 0 and 0.9999
If you multiple this returned number Value with 2
that would leave you a range
0...0.49 *2 ==> 0..0.99
0.50...0.999 *2 ==> 1....1,999

If you then do a Math.floor() that would lead to
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
0...0.49 *2 ==> 0..0.99 =>Math.floor() ==> 0
0.50...0.999 *2 ==> 1....1,999 => Math.floor() ==> 1

Thus the youHit variable has an associated _number Value of 0 OR 1
and you then use the Javascript truthy // falsy facility
https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Reference::

google search
=the book=
javascript variable site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
javascript return statement site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
=discussions=
javascript return statement site:stackoverflow.com
http://stackoverflow.com/questions/3717420/is-there-a-benefit-to-using-a-return-statement-that-returns-nothing
http://stackoverflow.com/questions/22134753/return-statement-in-javascript