Type Casting Math.random?

前端 未结 2 1414
野趣味
野趣味 2021-01-20 01:26

Had a look around the questions on this site and could not quite find the answer I was looking for about type casting the Math.random() method from double to in

2条回答
  •  萌比男神i
    2021-01-20 01:58

    This code:

    number = (int) Math.random() * 10; 
    

    first calculates this:

    (int) Math.random()
    

    Since Math.random() returns a number from 0 up to but not including 1, if you cast it to int, it will round down to 0. Then when you multiply 10 to 0 you get 0.

提交回复
热议问题