admin管理员组文章数量:1318979
For the different JavaScript implementations of Math.random
:
Putting aside memory and length issues, will the following eventually have an eternally repeating sequence of numbers (e.g. It only depends on an internal seed, and when that seed wraps back to its starting point the numbers will repeat)?
sequence = Math.random();
while(true){
sequence += ', ' + Math.random();
}
Will each client have the same repeating sequence (e.g. Clients don't incorporate client-specific data into the random number generation process)?
I ask because if the possible sequence of numbers is a limited subset, things like generating UUIDs with Math.random will have a much greater chance of collision.
For the different JavaScript implementations of Math.random
:
Putting aside memory and length issues, will the following eventually have an eternally repeating sequence of numbers (e.g. It only depends on an internal seed, and when that seed wraps back to its starting point the numbers will repeat)?
sequence = Math.random();
while(true){
sequence += ', ' + Math.random();
}
Will each client have the same repeating sequence (e.g. Clients don't incorporate client-specific data into the random number generation process)?
I ask because if the possible sequence of numbers is a limited subset, things like generating UUIDs with Math.random will have a much greater chance of collision.
- I don't think each client can have the SAME repeating SEQUENCE, assuming sequence meaning a series of numbers. You might eventually get a repeating number but sequence repeating seems unlikely to impossible – Huangism Commented Nov 26, 2014 at 15:28
- Well, there is the possibility of two clients having the exact same seed(their timestamp being exactly the same), but the chances are slim...for sure. I wonder what the seed generators are for Spidermonkey and V8... – Benjamin Trent Commented Nov 26, 2014 at 15:36
- Yes, it is guaranteed to repeat, and to repeat the same sequence given the same seed. DO NOT use Math.random() for generating UUIDs--you need a cryptographic RNG for that. – Lee Daniel Crocker Commented Nov 26, 2014 at 18:09
5 Answers
Reset to default 5From reading MDN:
Note: Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.
I would assume, that collisions are eventually possible.
This mdn doc for Math.random()
says that you can not rely on this to be truly secure.
But you could still try the alternative suggested window.crypto.getRandomValues()
but at the time I write this, it is still experimental.
The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
Note: Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.
It, mostly, depends on the seed generator of the underlying system. If two clients have the same exact seed, then the sequence will end up being the same...generally. There may be slight differences between the different implementations but the default falls back on the implementation in Java: Dig this SO question for additional info.
Generally the seed is a "bit better", aka slightly "more random" than just a timestamp.
Specifically:
V8
on Windows will use two different s_rand
calls and bit arithmetic to get the seed for the generator. If /dev/urandom
exists, it will use it instead. urandom
is farely good as it does not simply use a unix timestamp but environmental noise. If both options are not available, V8 will just use different time stamps and mathematically bine them. However, the sequence of random numbers is not directly pulled from Java, and will probably not have the exact same sequence as a FireFox client as getting the next random number uses a different mathematical formula.
Firefox does something very similarly and it looks like they lifted the definition from Java. As for the seed, again, its generation is very similar to that of V8, using s_rand
on windows /dev/urandom
when it is available, and falling back on timestamps when neither are available.
All in all, the generation is "pseudo-random" and if the calculation of the next random number is the same(Chrome and Firefox differ slightly on this) and the two sequences are started with the same exact seed, then of course, the two clients will have the exact same sequence of numbers. Statistically, the chances of this happening are insignificant, but conceivably, it indeed can happen.
Dig the following sources for some more in depth statistical, mathematical goodness.
Sources:
- Firefox implementation of math_random
- V8's implementation
- Breaking the Java random number generator
- Predicting the Seed in JS
All random number generators need a seed; otherwise they are just a list of numbers that seem kind of random but will repeat eventually. Javascripts Math.Random() does not accept a seed as an argument and instead relies on a built-in seed generator. Even though it is a psuedo-random number generator, because no one has control over where the seed actually starts, Math.random() shouldn't have any kind of predictable pattern.
Check http://bocoup./weblog/random-numbers/ for a bit more on this.
No, while nothing in puting is truly random, the algorithms that are used to create these "random" numbers are make it seem random so you will never get a repeating pattern. Most (I'm not sure about Math.random) randomising functions will get the current timestamp and use that as part of it's process and this is one of the main reasons you will not get repeating data in this case.
本文标签: javascriptWill Mathrandom repeatStack Overflow
版权声明:本文标题:javascript - Will Math.random repeat? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742057308a2418373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论