javascript 在javascript中生成随机整数的最快方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8775962/
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
What is the fastest way to generate a random integer in javascript?
提问by auroranil
Normally this is how you get a random number in javascript.
通常这是您在 javascript 中获得随机数的方式。
Math.random();
However this method seems to be inefficient when it comes to generating random integers.
然而,这种方法在生成随机整数时似乎效率低下。
Firstly, the random function has to generate a random decimal, like 0.1036098338663578, then it has to be multiplied to a suitable range (10.464593220502138). Finally, the floor function subtracts the decimals to produce the result (which in this case, 10).
首先,随机函数必须生成一个随机小数,如0.1036098338663578,然后它必须乘以一个合适的范围(10.464593220502138)。最后, floor 函数减去小数以产生结果(在本例中为10)。
var random_integer = Math.floor(Math.random()*101);
Is there a faster way to generate random integers in javascript?
有没有更快的方法在javascript中生成随机整数?
Edit1:
编辑1:
I am using this for creating a canvas HTML5 game. The FPS is about 50, and my code is pretty optimized, apart from generating a random number.
我正在使用它来创建一个画布 HTML5 游戏。FPS 大约是 50,我的代码非常优化,除了生成一个随机数。
回答by Dagg Nabbit
This code is faster... to type.
此代码更快...键入。
var random_integer = Math.random()*101|0;
It won't work right for huge numbers though.
但是,它不适用于大量数字。
(and it doesn't run any faster, at least not in chrome.)
(而且它的运行速度不会更快,至少在 chrome 中不会。)
You could achieve a much faster speed during the game if you generate the random numbers beforehand, though.
不过,如果您事先生成随机数,您可以在游戏中获得更快的速度。
for (var i=1e6, lookupTable=[]; i--;) {
lookupTable.push(Math.random()*101|0);
}
function lookup() {
return ++i >= lookupTable.length ? lookupTable[i=0] : lookupTable[i];
}
lookup
will rotate through an array with a million random integers. It is much fasterthan calling random
and floor
(of course, there is a "loading time" penalty up front from generating the lookup table).
lookup
将在一个包含一百万个随机整数的数组中旋转。它比调用快得多,random
并且floor
(当然,生成查找表会预先产生“加载时间”惩罚)。
回答by taskinoor
If you want to avoid floating point calculation then you can do that by writing your own pseudo random number generator. Hereis a list of well known pseudo random number generators (PRNG). Linear congruential generatoris the easiest one to implement and probably most effective in terms of performance too. However, you will need to understand the theory behind PRNGswell enough to write an effective one. That might not be worth of effort though. The JS implementation should be effective enough. At the end there is a high possibility that you will find Math.random()
is running faster than your code.
如果您想避免浮点计算,那么您可以通过编写自己的伪随机数生成器来实现。这是众所周知的伪随机数生成器 (PRNG) 的列表。线性同余生成器是最容易实现的,并且在性能方面可能也是最有效的。但是,您需要充分了解PRNG 背后的理论才能编写出有效的PRNG。但这可能不值得努力。JS 实现应该足够有效。最后,您很可能会发现Math.random()
它的运行速度比您的代码快。
回答by aleroot
Your way is the right way to retrive a random integer in javascript, don't worry about performance it will run fast.
您的方法是在 javascript 中检索随机整数的正确方法,不要担心它会快速运行的性能。
回答by Qyther
i mostly use
我主要使用
var a = Math.floor(Math.random((number you'd like to be minimum, (number you'd like to be maximum) * (number you'd like to be maximum);
回答by blake305
No, there is no easier or shorter way. You can create a function if you need to do it multiple times, though.
不,没有更简单或更短的方法。不过,如果您需要多次执行此操作,则可以创建一个函数。
回答by Marc Lundgren
const getRandomInt = (base = 10) => {
return Math.floor(Math.random() * base)
}