开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第1天,点击查看活动详情
Introduction
Pseudorandom number generator is the method of generating pseudorandom number by mathematical model or algorithm. It is also the most widely used and developed rapidly by modern information technology. The input to a pseudo-random number generator is called a seed, which itself is random and unpredictable. The output of the pseudo-random sequence is usually the deterministic function of the seed, so that each value in the sequence is generated by the conversion of the previous value, and the initial value of the function is the seed, The generation of pseudo - random number satisfies the chaotic property of high initial sensitivity. The pseudo random number tends to be more random in time than the random number obtained from physical sources, The output of the pseudo-random number generator has better statistical characteristics and higher generation rate.
Middle Square
The middle square method was developed by von Neumann. This method starts by taking an integer with bits, which is called the seed, and squares it to get the integer with bits (if there is less than bits, the high value will be 0). Then, take the middle bits of this bit as the next seed number, and normalize this number (that is, turn it into the real value of bits less than 1), which is the random number on the first . And so on, you get a bunch of random numbers. The iterative process can be described by the following formula:
where is output of pseudo-random. Although the middle square method can be implemented in simple order, it has many disadvantages. The cycle of the random sequence column depends too much on the initial value. Once the initial value is not selected well, the cycle degradation problem will appear. The matlab code of this algorithm is shown below:
clear;clc;
X = zeros(1, 1000);
Y = zeros(1, 1000);
X(1) = 1267;
Y(1) = X(1) / 10^4;
for i = 2:1000
X(i) = mod(floor(10^(-2)*X(i-1)^2), 10^4);
Y(i) = X(i) / 10^4;
end
plot(Y);
Linear Congruence
The linear congruent generator (LGG) is a widely used pseudo-random number generator, and its generation formula is as follows:
where represents the multiplier (greater than zero), represents the increment, and represents the modulus. If , the generator is often called the multiplicity Congruence generator (MCG). On the contrary, the generator is called mixing congruence generator. This algorithm is much better than the previous one. The matlab code of this algorithm is shown below:
clear;clc;
X = zeros(1, 1000);
X(1) = 2333;
a = 13; b = 17; m = 2333;
for i = 2:1000
X(i) = mod(X(i - 1)*a + b, m);
end
plot(X);