赞
踩
例1:创建一个长度为10,元素范围为1到10之间的数组
{
const arr = []
for(let i = 0; i < 10; i++){
const a = Math.random()*10 + 1;
arr.push(Math.floor(a))
}
console.log(arr);
}
例2:创建一个长度为100,元素范围为31到60之间的数组(包括31也包括60)
const arr = []
for(let i = 0; i < 100; i++){
const a = Math.random()*29 + 31;
arr.push(Math.round(a))
}
console.log(arr);
min ≤ n ≤ max : Math.round(Math.random()*(max-min)+min)
例2:创建一个长度为100,元素范围为31到60之间的数组(包括31不包括60)
{
const arr = []
for(let i = 0; i < 100; i++){
const a = Math.random()*29 + 31;
arr.push(Math.floor(a))
}
console.log(arr);
}
min ≤ n ≤ max : Math.floor(Math.random()*(max-min)+min)
例3:创建一个长度为100,元素范围为31到60之间的数组(不包括31包括60)
function randomFill() { const range = Math.round(Math.random()*29); let num = range + 31; if(range === 0){ return num + 1 }else{ return num } } const arr = [] for(let i = 0; i < 100; i++){ const num = randomFill(); arr.push(num) } console.log(arr);
例4:创建一个长度为100,元素范围为31到60之间的数组(不包括31不包括60)
function randomFill() { const range = Math.round(Math.random()*29); let num = range + 31; if(range === 0){ return num + 1 }else if(range === 29){ return num - 1 }else{ return num } } const arr = [] for(let i = 0; i < 100; i++){ const num = randomFill(); arr.push(num) } console.log(arr);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。