Linear Feedback Shift Register and Mason Rotation Algorithm

Source: Internet
Author: User
Tags random seed

Today we are mainly studyingMason Rotation AlgorithmIt is used to generate pseudo-random numbers. In fact, there are many methods to generate pseudo-random numbers, such as the linear same-remainder method,

China and France. However, the random number quality produced by these methods is often not very high.Mason Rotation AlgorithmHigh-quality pseudo-accompanying

Number of machines, and the efficiency is efficient, making up for the shortcomings of the traditional pseudo-random number generator.Mason Rotation AlgorithmThe longest cycle is taken from a prime number of Mason

This name isMason Rotation Algorithm. The common two are 32-bitMT19937-32And 64-bitMT19937-64.

 

BecauseMason Rotation AlgorithmYesLinear Feedback Shift Register (lfsr)Generates random numbers, so let's first understandLinear Feedback Shift Register.

 

First, the shift register contains two parts:

(1) level. Each level includes a bit, for example11010110Is8Generated by the level shift register

(2) feedback function. The feedback function of the Linear Feedback Shift Register is linear. The feedback function of the nonlinear feedback shift register is non-linear.

 

The maximum period of the sequence generated by a level shift register is, of course, this maximum period is closely related to the feedback function, and linear feedback function is implemented.

This is the result of selecting "some bits" for the shift register at this level for XOR. Here, the selection of "some bits" is very important to obtain linear inversion.

After the function is fed, the shift register moves one bit to the right each time, the rightmost end is used as the output, and the XOR or result of "some bits" is put as the input to the most

The person on the left end, so that all outputs correspond to a sequence, which is calledM Sequence, YesLongest linear shift register sequence.

 

The problem of selecting "some bits" has not been solved yet, so it is very important to select which bits should be used for uniqueness or to ensure the maximum cycle.

. The selected sequence composed of "some bits" is called the trunning sequence. The theory shows thatLfsrObtain the longest cycle, which consists of multiple

Item Addition1Must bePrimitive PolynomialsThat is to say, this polynomial is not allowed, for example.

 

The following uses a 4-bit linear feedback shift register as an example to describe how it works.

 

 

 

The values are1 0 0 0And select the feedback function. The following sequence is obtained:

 

 

We can see that the perimeter is15. In this period, All integers in the open interval are covered, and there is no fixed sequence, which is very good.

Random.

 

As mentioned before, if the cycle of the Mason Rotation Algorithm is, it indicates that it is19937The linear feedback shift register is based on32Bit

OfMT19937-32Only need to use32So why do we need to select the perimeter algorithm? This is because it is random.

 

The mason rotation algorithm is based on the linear feedback shift register, which always performs shift rotation. The cycle is a prime number of Mason, which is indeed a real name.

 

Code:

# Include <iostream> # include <string. h> # include <stdio. h> # include <time. h> using namespace STD; bool isinit; int index; int mT [624]; // 624*32-31 = 19937 void srand (INT seed) {Index = 0; isinit = 1; MT [0] = seed; For (INT I = 1; I <624; I ++) {int T = 1812433253 * (MT [I-1] ^ (MT [I-1]> 30) + I; MT [I] = T & 0 xffffffff; // get the last 32 bits} void generate () {for (INT I = 0; I <624; I ++) {// 2 ^ 31 = 0x80000000 // 2 ^ 31-1 = 0x7fffff int y = (MT [I] & 0x80000000) + (MT [(I + 1) % 624] & 0x7fffffff ); MT [I] = MT [(I + 397) % 624] ^ (Y> 1); If (Y & 1) MT [I] ^ = 2567483615 ;}} int rand () {If (! Isinit) srand (INT) Time (null); If (Index = 0) generate (); int y = MT [Index]; y = y ^ (Y> 11); y = y ^ (Y <7) & 2636928640); y = y ^ (Y <15) & 4022730752); y = y ^ (Y> 18); Index = (index + 1) % 624; return y;} int main () {srand (0 ); // set Random Seed int CNT = 0; For (INT I = 0; I <1000000; I ++) {If (RAND () & 1) CNT ++ ;} cout <CNT/10000.0 <"%" <Endl; return 0 ;}

 

In fact, random number functions in many languages have been implemented by the Mason rotation method, for examplePythonRandom Number Module inRandomThat is, the hacker used Mei.

Sen Rotation Algorithm to generate pseudo-random series,C ++ 11There are also random number generators implemented by the Mason Rotation Algorithm.

 

Application of Mason Rotation Algorithm in Information Fingerprint Technology

 

InBaiduOrGoogleIn such a search engine, crawlers need to determine the weight of the crawled webpage, which is achieved through information fingerprints. For details

Is to map each URL randomly128Binary, so that each URL only occupies16Bytes space.128This is the random number of bits.

The website information fingerprint can prove that the random number generated is good enough, so it is impossible to have the same information fingerprint of the two websites.

As it is impossible to have two people's fingerprints, the Mason rotation algorithm is an algorithm that generates high-quality pseudo-random numbers.

 

 

 

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.