JavaScript學習:JavaScript隨機 math.random()

Math.random() 返回 0(包括) 至 1(不包括) 之間的隨機數。
例:
Math.random(); // 返回隨機數(總是返回小於1的數)
JavaScript隨機整數 Math.random() 與 Math.floor() 一起使用用於返回隨機整數。
例:
Math.floor(Math.random() * 10); // 返回 0 至 9 之間的數
*11就返回0-10之間,以此類推。效果與Math.floor(Math.random() * 10) + 1; // 返回 1 至 10 之間的數 相同。
JavaScript函數始終返回介於min(包括)和max(不包括)之間的隨機數
例:
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
這個 JavaScript 函數始終返回介於 min 和 max(都包括)之間的隨機數。
例:
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
W3School JavaScript隨機

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章