js生成1到100的隨機數(shù)最簡單的實現(xiàn)方法
更新時間:2020年02月07日 08:58:37 作者:angryTom
在本篇文章里小編給大家整理了關于js生成1到100的隨機數(shù)最簡單的實現(xiàn)方法,有需要的朋友們可以學習下。
js生成1到100的隨機數(shù)
js生成隨機數(shù)使用math.random()函數(shù)
Math.random()
具體實現(xiàn):
1、定義一個random()函數(shù),原理是 隨機數(shù)和最大值減最小值的差相乘 最后再加上最小值。
function random(min, max) { return Math.floor(Math.random() * (max - min)) + min; }
2、使用方法
for (var i = 1; i <= 10; i++) { console.log(random(1, 100)); }
3、效果圖
以上就是腳本之家小編整理的相關內(nèi)容,感謝大家的學習。