JS Random number Seed

Source: Internet
Author: User

js random number seed

1 try to think about it, if in a certain scene, we do a game, the user to play half of the time to quit, so that the user next come in can choose to continue the last time to continue to play, then the problem now: User play progress and user's points and other simple description of data, we can record down, But in the game of obstacles, flying objects and a lot of decorative gadgets, they even start each time the user point of random output, to the canvas all things and their size, location, etc. are recorded, it is not necessary.

So the seed random number on the debut, if we on the canvas when the elements are randomly drawn, there is a seed value, all the elements on the page position, size, etc. are based on this seed to calculate, then wait until the second draw only need to pass this seed, you can reproduce the previous unfinished canvas elements.

Then this time, you will find that JS inside the math.random is not good enough to meet the needs, we continue to look at this code:






  





for (

}

Run as above code you will find that if the seed math.seed unchanged, then the generated random number will not change, oh, if the introduction of this function, then reproduce the game scene can be achieved, although still need to do more details, but the mechanism is guaranteed, the focus of this article is not to implement a game like this.

The focus of this article is: (Math.seed * 9301 + 49297)% 233280, why is this three values, and not the other exactly what is the three number of mysterious origin?

Like Math.seededrandom, this pseudo-random number generator is called the linear Same generator (LCG, Linear congruential Generator), and almost all of the runtime-supplied rand are LCG, shaped like:

in+1=ain+c (mod m)

Generates a sequence of pseudo-random numbers with a maximum period of M, ranging from 0 to m-1. To achieve this maximum cycle, you must meet:
1.c and M coprime
2.a-1 can be divisible by all factorization of M.
3. If M is a multiple of 4, A-1 must also be a multiple of 4

The above three is called the Hull-dobell theorem. As a pseudo-random number generator, the period is not large enough to be embarrassed to mix, so this is one of the requirements. So there are: a=9301, C = 49297, M = 233280 This set of parameters, the above three are all satisfied.

The above part comes from http://www.xiaomeiti.com/note/3589

2 seed the current time of the system as a random number initialized, and do not write the number to death.

If the same seed is used each time, the resulting random number is the same, and if each use a different seed, the resulting random number will be different.

JS Random number Seed

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: info-contact@alibabacloud.com 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.