Nov/27/2022-Classic Pseudo-random Number Generator

271 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 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 2N2N bits, which is called the seed, and squares it to get the integer with 4N4N bits (if there is less than 4S4S bits, the high value will be 0). Then, take the middle 2S2S bits of this 4s4s bit as the next seed number, and normalize this number (that is, turn it into the real value of 2S2S bits less than 1), which is the random number on the first (0,1)(0,1). And so on, you get a bunch of random numbers. The iterative process can be described by the following formula:

{ Xn+1=mod(floor(Xn10N),102N) Yn+1=Xn+1102N\left\{ \begin{aligned}   & {{X}_{n+1}}=\bmod (floor(\frac{{{X}_{n}}}{{{10}^{N}}}),{{10}^{2N}}) \\  & {{Y}_{n+1}}=\frac{{{X}_{n+1}}}{{{10}^{2N}}} \\ \end{aligned} \right.

where YY 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:

Xn+1=mod(aXn+b,m){{X}_{_{n+1}}}=\bmod (a{{X}_{n}}+b,m)

where aa represents the multiplier (greater than zero), bb represents the increment, and mm represents the modulus. If b=0b=0, 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);