要以math.floor和Math.random随机整数的方法详细的解决方案
Math.random():获得0 ~ 1的随机数Math.floor()方法将数字向下到最近的整数,并返回(小于等于等于最近的整数。)
实际上,返回值是这个数字的整数:
Math.floor(0.666 / 0)
math.floor(39.2783 / 39)
所以我们可以用math.floor(Math.random())得到的整数,你想要在一个范围。
例如,现在从1到52取一个随机数:
首先,Math.random(* 52) / /让我们能得到许多< 52 > = 0
然后添加1:Math.random(* 52)+ 1 / /现在这个号码> = 1和<53
使用math.floor积分
最后:Math.floor(Math.random)*(52 + 1)
这样就可以得到一个1到52的随机整数。