随机生成随机数的五种方法与区别

//第一种:是数字的取整,因为math.random()的取值范围是大于等于0,小于1,取不到1
   document.write(parseInt(Math.random()*3))//结果是0,1,2
//第二种:要想取到从1-3的随机数必须从要在产生随机数的后面加上1才可以从1开始取值,娶不到0;
       document.write(parseInt(Math.random()*3)+1)//结果是1,2,3
//第三种是数字的向上取整必须是大于0开始,永远取不到0
 document.write(Math.ceil(Math.random()*3))//结果是1,2,3;
//第四种是数字的向下取整,必须是从0开始,最后的一个数字是取不到你想要的区间范围的最后的一个数据
      document.write(Math.floor(Math.random()*3))//结果是0,1,2
//第五种是数字的四舍五入取整随机产生的随机数有可能是大于0.5就会向前一位进1
 document.write(Math.round(Math.random() * 3));//结果是0,1,2,3
//第六种生成随机数字的方法
document.write(Math.random().toString().slice(-6))
//第七种生成的随机数里面有数字和字母的方法
document.write(Math.random().toString(36).slice(-6))
posted @ 2021-04-16 11:22  干饭吧  阅读(639)  评论(0编辑  收藏  举报