Objective
In the front-end development, especially game development, often use random numbers, then we will be the first time to think: Math.random
, we look at the following code slightly:
for (var i= 0; i<10; i++) {Document.writeln (Math.random () + "<br/>");}
Running code like this, it does generate 10 different numbers, of course you can generate more, look good, if only so, then this article is not necessary to write.
Example
Try to think, if in a certain scene, we do a game, users play to half of the time to quit, so that the next time the user can choose to continue to play the last progress, then now the problem is: the user to play the progress and user points such as simple description of the data, we can record down, But the obstacles in the game, flying objects and a lot of decorative gadgets, they even began to randomly output each user point, to the canvas all the things and their size, position, etc. are recorded, it is not necessary.
So the random number of seeds is on the scene, if we have a seed value on the canvas when the elements are randomly drawn, the position and size of all the elements on the page are counted according to the seed, then the canvas elements that were previously unfinished can be reproduced by the time the second drawing is passed only to the seed.
So this time, you will find JS inside the math.random is not good enough to meet the needs, we continue to look at this code:
Copy Code code as follows:
Math.seed = 5; Math.seededrandom = function (max, min) {max = max | | 1; min = min | | 0; Math.seed = (Math.seed * 9301 + 49297)% 233280; var rnd = math.seed/233280.0; return min + rnd * (max-min); }; for (var i= 0; i<10; i++) {Document.writeln (Math.seededrandom () + "<br/>");}
Run the code as above you will find that if the seed math.seed unchanged, then the generated random number is not going to change, oh, if the introduction of this function, then reproduce the game scene can be achieved, although more details need to be done, but the mechanism is guaranteed, the focus of this article is not to achieve a game like this.
The focus of this article is : (math.seed * 9301 + 49297)% 233280, why are these three values, and not the other in the end what is the mystery of these three numbers?
Such pseudorandom number generators as math.seededrandom are called linear with the rest of the generator (LCG, Linear congruential Generator), and almost all of the runtime's supplied Rand is LCG, in the form of:
The maximum periodic m of the generated pseudo-random number sequence, ranging from 0 to m-1. To reach this maximum cycle, you must meet:
1.c and M coprime
2.a-1 can be divisible by all decomposition of M
3. If M is a multiple of 4, A-1 must also be a multiple of 4
The above three articles are called Hull-dobell theorems. As a pseudo random number generator, the cycle is not big enough to be embarrassed to mix, so this is one of the requirements. So there is: a=9301, C = 49297, M = 233280 of this set of parameters, the above three are all satisfied.
Summarize
The above is about the seed random number in JS how to implement and role of the content, I hope to give JavaScript learners help