C# Random Number Generation Mersenne Twister Matset Twister Algorithm Mersenne Twister Algorithm

NuGet installation MathNet.Numerics
reference:
using MathNet.Numerics.Random;

        /// <summary>
        /// 包括lower,不包括upper
        /// </summary>
        /// <param name="lower"></param>
        /// <param name="upper"></param>
        /// <param name="randomResult"></param>
        public static void GetRandom(int lower,int upper,out int randomResult)
        {
    
    
            var random = new MersenneTwister(RandomSeed.Robust()); 
            randomResult= random.Next(lower, upper);
        }

The Mersenne Twister algorithm is a pseudo-random number generator whose name comes from the fact that its period length is usually taken from the Mersenne prime number. It was developed by Makoto Matsumoto (Matsumoto) and Takuji Nishimura (Nishimura) in 1997 and is based on linear regeneration of matrices over finite binary fields. Its main function is to generate pseudo-random numbers and correct many defects of the ancient random number generation algorithm.

The Mersenne Twister algorithm uses a linear feedback shift register (LFSR) to generate random numbers. The feedback function of this register is a simple XOR of certain bits, also known as the tap sequence. An n-bit LFSR can generate a 2 n-1 bit long pseudo-random sequence before repeating. Only the LFSR with a specific tap sequence can pass through all 2 n-1 internal states to generate a 2^n-1 bit long pseudo-random sequence. This output sequence is called the m sequence. In order for the LFSR to be a maximally periodic LFSR, the polynomial formed by the tap sequence plus a constant 1 must be a primitive polynomial.

The Mersenne Twister algorithm has some significant advantages. The random numbers it generates are of good quality and highly random, can be easily implemented on a computer, and occupy less memory. Compared with other pseudo-random number generators that have been used, it runs faster, has a longer period, can reach 2^19937-1, and has the property of 623-dimensional uniform distribution. For general applications, this cycle length is sufficient. In addition, its sequence correlation is small and it can pass many randomness tests. In the fields of computer science and engineering, the Mersenne Twister algorithm is widely used in situations where random numbers need to be generated.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_34677276/article/details/132864159