**數組
function getRandom() {//獲取0~1之間隨機數 return Math.random(); }
function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive } //此示例返回指定值之間的隨機整數。該值不低於min(或者大於minif 的下一個整數min不是整數),而且小於(但不等於)max。
要求dom
// 隨機生成長度爲 10 的數組。 // 每項的類型爲 object 對象 // 其 x 屬性的值 大於或等於 5 小於 12 // 其 y 屬性的值 大於或等於 12 少於 18
var arr =[]; //建立返回值的新數組 for(var i = 0;i<10;i++){ //隨機生成長度爲10的數組,使用for循環十次 var tmp={}; //創建空值 let x = Math.floor(Math.random()*(12-5)+5); let y = Math.floor(Math.random()*(18-12)+12); tmp["x"] = x; tmp["y"] = y; arr.push(tmp); }console.log(arr)
歡迎各位大牛前來增長回答/請不要需改原有回答和條件並在此線下提交更優秀的回答code