JavaScript Math(算數)
開發工具與關鍵技術:JavaScript Math算數
作者:聽民謠的老貓
撰寫時間:2019/2/6 17:15
數字驗證碼是我們接觸的比較多的一種身份驗證方法,有短信接收的驗證碼和網站刷新的驗證碼,是系統自動生成的4到6位數的隨機數字。
我在mvc學習中接觸的 Math(算數)對象就可以做到驗證碼的生成。
- Math(算數)對象的作用是:執行常見的算數任務。
- Math(算數)對象有很多在這我就講用於生成驗證碼的兩種。
Math.round() | 小數化整數(取最接近的整數) |
---|---|
Math.random() | 0~1中生成一個隨機數 |
- Math.round()代碼與在線調試
- Math.random() 代碼與在線調試
Math.round()和Math.random()的組合使用,生隨機數字
-
Math.round(Math.random()*9)
生成0~9之間的隨機數
-
**
Math.round(Math.random()*9)
**由Math.round()和Math.random()組合生成。Math.random()是隨機生成0~1之間的小數,將它*9就隨機生成了0到9之間的小數。再用Math.round()把0到9小數化成0到9整數。就是隨機數的生成方法。 -
4位數驗證碼的生成_____代碼與在線調試:
-
只要將代碼
Math.round(Math.random()*9)
用String字符串的形式包裹,然後加上3個同樣的Math.round(Math.random()*9)
代碼塊,就生成了4位數的驗證碼。快去試試吧! -
一定要用String字符串的形式包裹,不然
Math.round(Math.random()*9)
代碼塊就會相加生成和!!!