javascript 随机彩票号码生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32308691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random lottery number generator
提问by kogigr
What I'm trying to do is generate 6 random numbers, five in a range of 1-45 and one in a range of 1-25for a Greek lottery game (Tzoker). The first 5 numbers should be unique. By pressing a button, I want to add these numbers to a div using jQuery (I have some working code for this part).
我想要做的是为希腊彩票游戏(Tzoker)生成 6 个随机数,其中 5 个在 1-45 的范围内,一个在 1-25 的范围内。前 5 个数字应该是唯一的。通过按下一个按钮,我想使用 jQuery 将这些数字添加到一个 div(我有这部分的一些工作代码)。
I thought it would be pretty easy using a loop, but I've found myself unable to check if the number generated already exists. The loop would only contain the first 5 numbers, because the last number can be equal to one of the other 5.
我认为使用循环会很容易,但我发现自己无法检查生成的数字是否已经存在。循环将只包含前 5 个数字,因为最后一个数字可以等于其他 5 个数字之一。
回答by mik01aj
Let me propose you some simpler solution.
让我为您推荐一些更简单的解决方案。
- Make a list of all numbers from 1 to 45.
- Sort the list using
Math.random
(plus minus something, read the docs ofArray.sort
to find out) as the comparison function. You will get the list in random order. - Take 5 first items from the list.
- Then, when you already have the numbers, put them all into your div.
- 列出从 1 到 45 的所有数字。
- 使用
Math.random
(加减某些内容,阅读文档Array.sort
以找出答案)作为比较函数对列表进行排序。您将按随机顺序获得列表。 - 从列表中取出 5 个第一项。
- 然后,当您已经有了数字时,将它们全部放入您的 div 中。
This way you don't mix your logic (getting the numbers) with your presentation (putting stuff into the DOM).
这样,您就不会将逻辑(获取数字)与演示文稿(将内容放入 DOM)混合。
I leave the implementation as an exercise for the reader. :)
我将实现留给读者作为练习。:)
回答by LinkinTED
Like this?
像这样?
$(function() {
$('button').on('click', function(e) {
e.preventDefault();
var numArray = [];
while( numArray.length < 5 ) {
var number = Math.floor((Math.random() * 45 ) + 1);
if( $.inArray( number, numArray ) == -1 ) {
numArray.push( number );
}
}
numArray.push( Math.floor((Math.random() * 25 ) + 1) );
$('div').html( numArray.join("<br />") );
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Generate</button>
<div></div>
回答by mik01aj
While this might be not exactly what you were asking for, if you would use lodash, this would be as simple as:
虽然这可能不是您所要求的,但如果您使用lodash,这将非常简单:
_.sample(_.range(1, 46), 5) // the 5 numbers from 1..45
_.random(1, 26) // one more from 1..25
This is why functional programmingis so cool. You can read for example Javascript Allongeto find out more.
这就是函数式编程如此酷的原因。您可以阅读例如Javascript Allonge以了解更多信息。
回答by Didier Aupest
var tzoker = $("#tzoker");
var results = $("#results");
tzoker.click(function() {
results.empty();
var properResults = [];
var rand = 0;
var contains = false;
for (i = 1; i < 7; i++) {
do
{
(rand = Math.floor((Math.random() * (i != 6 ? 45 : 25)) + 1));
contains = properResults.indexOf(rand) > -1;
} while(contains)
results.append("<br />", rand, "<br />");
properResults.push(rand);
}
});
Here is the main idea of a solution. You can define the max value as a parameter for the random. Then, check the existence of the item in a simple array with only the data you want.
这是解决方案的主要思想。您可以将最大值定义为随机参数。然后,仅使用您想要的数据检查简单数组中是否存在该项目。
回答by Alessio Cantarella
You may use a general function which generates random numbers from 1
to maxValue
, and adds them to an array only if they don't exist. Then, to display, cycle through the array items and append them to #randomNumbers
.
您可以使用从1
to生成随机数的通用函数maxValue
,并且仅当它们不存在时才将它们添加到数组中。然后,要显示,循环遍历数组项并将它们附加到#randomNumbers
.
HTML
HTML
<div id="randomNumbers"></div>
JS(with jQuery)
JS(使用 jQuery)
var randomNumbersArray = [];
$(function() {
generateRandomNumbers();
displayRandomNumbers();
});
function generateRandomNumbers() {
for (i = 0; i < 5; i++) {
generateRandomNumberFrom1To(45);
}
generateRandomNumberFrom1To(25);
}
function generateRandomNumberFrom1To(maxValue) {
var randomNumber;
do {
randomNumber = Math.ceil(Math.random() * maxValue);
} while ($.inArray(randomNumber, randomNumbersArray) > -1);
randomNumbersArray.push(randomNumber);
}
function displayRandomNumbers() {
for (i in randomNumbersArray) {
$("#randomNumbers").append(randomNumbersArray[i] + "<br>");
}
}