Explain Math.floor(Math.random())

前端 未结 6 1192
轻奢々
轻奢々 2020-12-19 07:52

I have seen many places using Math.floor() and Math.random()

like below

$(\'a.random-color\').hover(function() { //mouseove         


        
6条回答
  •  长情又很酷
    2020-12-19 08:06

    Math.random will give you a floating point number between 0 (inclusive) and 1 (exclusive).

    Multiplying that by 256 will give you a number in the range 0 (inclusive) through 256 (exclusive), but still floating point.

    Taking the floor of that number will give you an integer between 0 and 255 (both inclusive).

    It's the integer from 0 to 255 that you need to construct RGB values like rgb(72,25,183).

提交回复
热议问题