blog
原创

JavaScript——使用Math对象生成0~100的随机数

1.使用random()方法生成随机小数,范围[0,1)

Math.random();

2.对1的结果乘以100,随机数范围[0,100)

Math.random() * 100;

3.使用ceil()方法对2的结果进行向上取整

Math.ceil(Math.random() * 100);

或使用floor()方法进行向下取整,然后结果+1

Math.floor(Math.random() * 100) + 1;

4.输出结果

document.write(Math.ceil(Math.random() * 100) + "<br/>");
document.write(Math.floor(Math.random() * 100) + 1);
JavaScript
JavaScript案例
  • 作者:Melonico
  • 发表时间:2021-03-15 15:36
  • 更新时间:2021-03-15 15:36

评论

暂无评论,快来发表第一个评论吧!
留言
TOP