使用JavaScript编写一个双色球号码的程序
以使用JavaScript编写一个双色球号码的程序。以下是一个简单的示例,它使用Math.random()函数生成随机数字,并将它们格式化为双色球号码的格式:
function generateLotteryNumbers() {
var whiteBalls = [];
var redBall;
// Generate 5 unique random numbers between 1 and 69 for the white balls
while (whiteBalls.length < 5) {
var randomNumber = Math.floor(Math.random() * 69) + 1;
if (whiteBalls.indexOf(randomNumber) === -1) {
whiteBalls.push(randomNumber);
}
}
// Generate a random number between 1 and 26 for the red ball
redBall = Math.floor(Math.random() * 26) + 1;
// Format the numbers as a string in the format "5 23 37 42 63 + 21"
var formattedNumbers = whiteBalls.sort(function(a, b) {
return a - b;
}).join(' ') + ' + ' + redBall;
return formattedNumbers;
}
console.log(generateLotteryNumbers());
这个函数将生成一个随机的双色球号码,并将其打印到控制台。您可以根据需要修改此函数,以便将号码显示在页面上或以其他方式使用它。
修改为中国福彩的33选1和14选1
function generateLotteryNumbers() {
var whiteBalls = [];
var redBall;
// Generate 5 unique random numbers between 1 and 69 for the white balls
while (whiteBalls.length < 6) {
var randomNumber = Math.floor(Math.random() * 33) + 1;
if (whiteBalls.indexOf(randomNumber) === -1) {
whiteBalls.push(randomNumber);
}
}
// Generate a random number between 1 and 26 for the red ball
redBall = Math.floor(Math.random() * 14) + 1;
// Format the numbers as a string in the format "5 23 37 42 63 + 21"
var formattedNumbers = whiteBalls.sort(function(a, b) {
return a - b;
}).join(' ') + ' + ' + redBall;
return formattedNumbers;
}
console.log(generateLotteryNumbers());
THE END
二维码