Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

后端 未结 4 670
南方客
南方客 2021-01-05 15:39

By multiplying the random number (which is between 0 and 1) by 5, we make it a random number between 0 and 5 (for example, 3.1841). Math.floor() rounds this number down to a

4条回答
  •  执笔经年
    2021-01-05 16:28

    Note that:

    • 0 <= Math.random() **<** 1
    • Math.floor(x.yz) = x

    And therefore, the number given is a integer in the interval:

    x = Math.floor((0..0.999999999) * 5 + 1)
    x = (0..4) + 1
    

提交回复
热议问题