<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Page Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <script> console.log('雙色球!紅色球:' + radom(33, 6) + '藍色球是:' + Math.ceil(Math.random() * 16)); console.log('大樂透!紅色球:' + radom(35, 5) + '藍色球是:' + radom(12, 2)); /* * @method 生成隨機的幾個數 * @param {num, num} 數值的最大數,共幾位數 * @return {array} 返回隨機生成的從小到大排列的數組 */ function radom(max, len) { var redNum = [], radomNum; while (redNum.length < len) { radomNum = Math.ceil(Math.random() * max); if (redNum.indexOf(radomNum) < 0) { redNum.push(radomNum); } } // 生成的數據以從小到大的順序排列 redNum.sort(function compare(a, b) { return a - b; }) return redNum; } </script> </body> </html>