Chrome V8 Engine Series essay (1): Math.random () Function overview

Source: Internet
Author: User

Let's take a look at the graph, which is a map of the values of the V8 engine version 4.7 and 4.9 math.random () functions, as I can understand. From that, maybe you think it's a QR code? In fact, this picture tells us one truth, the second graph point distribution is more dense, that is to say the Math.random () function can represent more numbers, everyone in. NET certainly also used the GUID bar, as to why the GUID will never repeat, have you ever thought about it?

Let's take a look at what the authorities are explaining. Math.random (), it returns a positive number, which is between 0~1 and fluctuates in a pseudo-random way within this range. The Math.random () function is also used extensively in JS. Its core principle is to use (pseudo-random number generator PRNG), random number derived from an internal state, through a fixed algorithm to get each different random number. So for a known internal initial state, the random number of the queue can be calculated, since the bit size n is limited, the PRNG generator will repeat its own previous work. The maximum range of periodic upper bounds for a displacement map is 2n.

Let's take a look at the PRNG (pseudo-random number) generation algorithm. Pseudo-random number, why is it not a real random number? Because the random number is never repeated! Pseudo-random number is only "a group of very close to the number", in our mathematics, the teaching is to approach a number, but not equal to a certain number, that is, its "limit" equals a certain number, the University of Mathematics has said. Here is a mathematical explanation:

    • -Probability distribution on top (when on burrelë domain).
    • -Non-empty burrelë field, for example, the left side means that the t<=0,t is a real number, and if it is not defined, it may be or be appended to the context.
    • A non-empty collection (not necessarily in the burrelë domain) is a collection that is between and within itself. Assuming P is evenly distributed between (0,], then a may also be between (0,1], if a is not defined, then it is assumed to be contained in the support of P and contained within it, and depends on the context.
    • We call a function: (When a positive set, for the pseudo-random number generator, for a given case and only if in a)

The above introduction is not dizzy out? It doesn't matter, the above only lets you have a general understanding of the random algorithm, does not require you to fully understand (in fact, I do not understand). In fact, about the PRNG algorithm has a lot of implementation, the most famous when Mersenne-twister and LCG. Each of the different algorithms has its own characteristics, a bit even a disadvantage. Take it for granted: in fact, only a very small amount of time can be used to complete the calculation of the entire process, as well as long-period calculation. When performance, memory usage, and calculation cycles can be easily estimated and calculated, the quality of the random distribution will be more difficult to determine and the test will be more difficult.

The following code merges the 2 16-bit State (status code) into one. The 32-bit numbers are converted to floating-point types and presented to us in the form of 0~1.

uint32_t state0 = 1;uint32_t state1 = 2;uint32_t mwc1616 () {  state0 = 18030 * (State0 & 0xFFFF) + (State0 >> );  State1 = 30903 * (state1 & 0xFFFF) + (state1 >>);  return state0 << + (state1 & 0xFFFF);

MWC1616 algorithm uses a small amount of memory, and fast running speed, but its quality (quality) is not to be flattering, its characteristics are as follows:

    • The range of numbers can be raised from 232 to 252 and let it be only between 0~1.
    • The more important state1 depends on the value of the STATE0 (see code above).
    • The maximum span of a number can reach 232 but if a bad initial state is selected, the cycle length is reduced to less than 40 million.

Because 64 people will have a lot of problems (Google researcher said), Google adopted a new xorshift128+ algorithm, look at the name everyone knows, is 128 bit, the period is 2128-1.

uint64_t state0 = 1;uint64_t state1 = 2;uint64_t xorshift128plus () {  uint64_t S1 = state0;  uint64_t s0 = state1;  State0 = S0;  S1 ^= S1 <<;  S1 ^= S1 >>;  S1 ^= S0;  S1 ^= s0 >>;  State1 = S1;  return state0 + state1;}

In fact, through this article, we can only have a general understanding of this function, but I can let you understand a little, read this article, you will miss IE6? O (∩_∩) o haha ~

Chrome V8 Engine Series essay (1): Math.random () Function overview

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.