測試正確:
var arr = [40,48,50,58];
var min = 0; var max = 4; var rand = Math.floor(Math.random()*(max-min+1))+min;
console.log(arr[rand]) // 0,1,2,3,4,5,4,1,2,4,10
Math.ceil(); //向上取整。
Math.floor(); //向下取整。
Math.round(); //四舍五入。
Math.random(); //0.0 ~ 1.0 之間的一個偽隨機數。【包含0不包含1】 //比如0.8647578968666494
Math.ceil(Math.random()*10); // 獲取從1到10的隨機整數 ,取0的概率極小。
Math.round(Math.random()); //可均衡獲取0到1的隨機整數。
Math.floor(Math.random()*10); //可均衡獲取0到9的隨機整數。
Math.round(Math.random()*10); //基本均衡獲取0到10的隨機整數,其中獲取最小值0和最大值10的幾率少一半。
① parseInt(Math.random()*(m-n)+n) // 生成 [n,m),包含n但不包含m的正整數:
② parseInt(Math.random()*(m-n)+n)+1 // 生成 (n,m],不包含n但包含m的正整數:
③ parseInt(Math.random()*(m-n+1)+n) // 生成 [n,m],包含n和m的隨機數:
④ parseInt(Math.random()*(m-n-1)+n+1) // 生成 (n,m),不包含n和m的正整數:
parseInt(10*Math.random()) //輸出0~9之間的隨機整數
Math.floor(Math.random()*10+1) //輸出1~10之間的隨機整數