Cool
Cool
Published on 2021-05-07 / 29 Visits
0
0

javascipt 随机数

方法描述
ceil(x)对数进行上舍入,即向上取整。
floor(x)对 x 进行下舍入,即向下取整。
round(x)四舍五入。
random()返回 0 ~ 1 之间的随机数,包含 0 不包含 1。

{card-default label="随机数" width="100%"} Math.ceil(Math.random()*10); // 获取从 1 到 10 的随机整数,取 0 的概率极小。

Math.round(Math.random()); // 可均衡获取 0 到 1 的随机整数。

Math.floor(Math.random()*10); // 可均衡获取 0 到 9 的随机整数。

Math.round(Math.random()*10); // 基本均衡获取 0 到 10 的随机整数,其中获取最小值 0 和最大值 10 的几率少一半。 {/card-default}


Comment