例如:要生成8个唯一的随机数并将其存储到数组中,只需执行以下操作:
var arr = [];while(arr.length < 8){ var r = Math.floor(Math.random() * 100) + 1; if(arr.indexOf(r) === -1) arr.push(r);}console.log(arr);
例如:要生成8个唯一的随机数并将其存储到数组中,只需执行以下操作:
var arr = [];while(arr.length < 8){ var r = Math.floor(Math.random() * 100) + 1; if(arr.indexOf(r) === -1) arr.push(r);}console.log(arr);