<!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>