19、js获取0-1之间的随机数,获取1-10之间的随机数

js获取0-1之间的随机数,获取1-10之间的随机数

1.js

 

//获取0-1之间的随机数

var num = Math.random();
  1. console.log(num);
  2. //获取1-10之间的随机数
  3. var num = Math.floor(Math.random() * 10+ 1);
  4. console.log(num);

2.获取两个数之间的随机整数

 

function getRandomNumberByRange(start, end) { 

return Math.floor(Math.random() * (end - start) + start)

}

getRandomNumberByRange(0,100)

效果:

原文地址:https://www.cnblogs.com/dream111/p/13466750.html