Javascript 用 jQuery 随机化数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4306105/
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
Randomize numbers with jQuery?
提问by omnix
Is there a simple jQuery way to create numbers randomly showing then a number 1 -6 is choosing after a few seconds? [Like dice]
是否有一种简单的 jQuery 方法来创建随机显示的数字,然后在几秒钟后选择一个数字 1 -6?[喜欢骰子]
回答by casablanca
This doesn't require jQuery. The JavaScript Math.random
function returns a random number between 0 and 1, so if you want a number between 1 and 6, you can do:
这不需要jQuery。JavaScriptMath.random
函数返回一个 0 到 1 之间的随机数,所以如果你想要一个 1 到 6 之间的数,你可以这样做:
var number = 1 + Math.floor(Math.random() * 6);
Update:(as per comment) If you want to display a random number that changes every so often, you can use setInterval
to create a timer:
更新:(根据评论)如果您想显示一个经常变化的随机数,您可以使用它setInterval
来创建一个计时器:
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
$('#my_div').text(number);
},
1000); // every 1 second
回答by cambraca
You don't need jQuery, just use javascript's Math.random
function.
您不需要 jQuery,只需使用 javascript 的Math.random
功能。
edit: If you want to have a number from 1 to 6 show randomly every second, you can do something like this:
编辑:如果你想每秒随机显示一个从 1 到 6 的数字,你可以这样做:
<span id="number"></span>
<script language="javascript">
function generate() {
$('#number').text(Math.floor(Math.random() * 6) + 1);
}
setInterval(generate, 1000);
</script>
回答by Gabriele Petrioli
Others have answered the question, but just for the fun of it, here is a visual dice throwing example, using the Math.random
javascript method, a background image and some recursive timeouts.
其他人已经回答了这个问题,但只是为了好玩,这里是一个视觉掷骰子的例子,使用Math.random
javascript 方法、背景图像和一些递归超时。
回答by FatherStorm
function rollDice(){
return (Math.floor(Math.random()*6)+1);
}
回答by McStretch
Javascript has a random()
available. Take a look at Math.random().
Javascript 有一个random()
可用的。看看Math.random()。
回答by David H.
Coding in Perl, I used the rand() function that generates the number at random and wanted only 1, 2, or 3 to be randomly selected. Due to Perl printing out the number one when doing "1 + " ... so I also did a if else statement that if the number generated zero, run the function again, and it works like a charm.
在 Perl 中编码,我使用 rand() 函数随机生成数字,并且只希望随机选择 1、2 或 3。由于 Perl 在执行 "1 + " 时打印出数字 1 ... 所以我还做了一个 if else 语句,如果数字生成零,则再次运行该函数,它就像一个魅力。
printing out the results will always give a random number of either 1, 2, or 3.
打印出结果总是会给出一个随机数 1、2 或 3。
That is just another idea and sure people will say that is newbie stuff but at the same time, I am a newbie but it works. My issue was when printing out my stuff, it kept spitting out that 1 being used to start at 1 and not zero for indexing.
这只是另一个想法,肯定人们会说这是新手,但同时,我是新手,但它有效。我的问题是在打印我的东西时,它一直吐出 1 用于从 1 开始而不是用于索引的零。