Preface
In front-end development, especially game development, random numbers are often used, so we will think of: Math.random , let’s take a look at the following code:
for (var i= 0; i<10; i++) { document.writeln(Math.random() +<br />); }Running the above code does generate 10 different numbers. Of course you can generate more, which looks pretty good. If that's all, then there is no need to write this article.
Example
Try to think about it. If we make a game in a certain scenario and the user exits halfway through the play, so that the user can choose to continue the previous progress next time he comes in. Then the problem is now: we can record simple description data such as user play progress and user points, but the obstacles, flying objects and many decorative gadgets drawn in the game, they even start to output randomly every time the user points starts to record everything on the canvas, their size, location, etc., which is really unnecessary.
So the random seed number appears. If we have a seed value when the element is randomly drawn on the canvas, and the position, size, etc. of all elements on the page are calculated based on this seed, then when the second drawing is drawn, we only need to pass in this seed to reproduce the previously unfinished canvas elements.
Then at this time, you will find that the Math.random that comes with JS is not working well and cannot meet the needs. Let's continue to look at this code:
The code copy is as follows:
Math.seed = 5; Math.seedRandom = 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.seedRandom() +<br />); }
Run the above code and you will find that if the seed Math.seed remains unchanged, the generated random number will not change. Oh, if this function is introduced, then reproducing the game scene can be achieved. Although more details are required, the mechanism can be guaranteed. The focus of this article is not to implement such a game.
The focus of this article is : (Math.seed * 9301 + 49297) % 233280 , why are these three values, not other, and what mysterious origins do these three numbers have?
A pseudo-random number generator like Math.seedRandom is called a linear congruent generator (LCG, Linear Congruential Generator). Almost all rands provided by the runtime library use LCG, which is like:
I n+1=aI n+c(mod m)
The maximum period m of the generated pseudo-random number sequence is between 0 and m-1. To achieve this maximum cycle, it is necessary to meet:
1.C and m are mutually exclusive
2.a - 1 can be divided by all prime factors of m
3. If m is a multiple of 4, a - 1 must also be a multiple of 4
The above three are called Hull-Dobell theorem. As a pseudo-random number generator, it is embarrassing to get stuck if the cycle is not large enough, so this is one of the requirements. Therefore, the following are: a=9301, c = 49297, m = 233280. All of the above three items are satisfied.
Summarize
The above is an introduction to how to implement and function seed random numbers in JS. I hope it will be helpful to JavaScript learners.