javascript Math取整&獲取隨機數


1.方法介紹
Math.ceil(n) 上取整,大於等於n返回與它最接近的整數
Math.floor(n) 下取整,小於等於n返回與它最接近的整數
Math.round(n) 四舍五入取整
Math.random() 獲取0~1的隨機數


2.獲取m到n的隨機數

Math.floor(Math.random()*(m-n+1))+n

或者

Math.ceil(Math.random()*(m-n))+n


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM