Details about how to implement seed random numbers and their functions in JS, and details about seed random numbers in js

Source: Internet
Author: User
Tags random seed

Details about how to implement seed random numbers and their functions in JS, and details about seed random numbers in js


In front-end development, especially game development, random numbers are often used, so we will first think:Math.randomLet's take a look at the following code:

for (var i= 0; i<10; i++) { document.writeln(Math.random() +"<br />"); }

Run the above Code to generate 10 different numbers. Of course, you can generate more, which looks good. If this is the case, then there is no need to write this article.


Try to think about it. If we make a game in a certain scenario, the user will quit halfway through the game, so that the user can choose to continue playing the last game in the next time, now the question is: we can record the progress of the user's play and the user's points and other simple descriptive data, but the obstacles, flying articles, and many decorative gadgets drawn in the game, they even start to output randomly at each user point. It is unnecessary to record all the items on the canvas, their size and position.

As a result, the random seed number will pop up. If we randomly draw elements on the canvas, there will be a seed value, the position and size of all elements on the page are calculated based on this seed. After the second painting, you only need to input this seed to reproduce the unfinished Canvas elements.

At this time, you will find that the built-in Math. random in JS cannot meet your needs. Let's continue to look at this Code:

Copy codeThe Code is 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 above Code and you will find that if the seed Math. if seed remains unchanged, the random number generated will not change. Oh, if this function is introduced, you can reproduce the game scenario. Although more details are needed, but the mechanism is guaranteed. The focus of this article is not to implement such a game.

The focus of this article is:( Math. seed * 9301 + 49297) % 233280Why are these three values, rather than the other three numbers, mysterious origins?

A pseudo-random number Generator like Math. seededRandom is called a Linear coequal Generator (LCG, Linear Congruential Generator). Almost all the rand provided by the runtime libraries use LCG, for example:

I n+1=aI n+c(mod m)

The maximum period of the pseudo-random number sequence is m, ranging from M-1. To reach the maximum cycle, the following conditions must be met:
1. Interaction between c and m

2. a-1 can be divisible 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 the Hull-Dobell theorem. As a pseudo-random number generator, the cycle is not big enough, so this is one of the requirements. Therefore, the parameters a = 9301, c = 49297, m = 233280 are all met.


The above describes how to implement and use seed random numbers in JS. I hope it will be helpful to JavaScript learners.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.