Ways to create random Boolean. Learn Basic JavaScript by Building a Role Playing Game - Step 147

I have researched some other ways to create random boolean and understood the concepts behind them but I need help explaining Math.random() > .2.
Why is it .2 instead of just 2? Thank you.

Also is there any difference between using

Math.random() > .2

Math.floor(Math.random() * 2)

Math.random() >= 0.5

to return for boolean? Thank you.

Do you understand why this will return false approximately 50% of the time and true approximately 50% of the time?

If so, then I think you should be able to figure out what the percentages will be for:

Math.random() > .2

(Hint: It’s not 50/50.)

And also why using 2 instead of .2 wouldn’t make sense.

It is because Math.random() can return any value between 0 and 1, so there is a roughly 50/50 chance that the result will be above or below 0.5.

I now see why Math.random() > 2 will not make sense…
I still do not understand what .2 means. I could not find the answer from Google. It is not 0.2 is it?

If there is a 50/50 chance that Math.random() will return a number greater than or equal to 0.5, then what are the odds it will return a number greater than or equal to 0.2?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.