Five commonly used random number generators-MATLAB implementation

Publisher:考古专家Latest update time:2022-06-30 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Five commonly used random number generators include the square median generator, the product median generator, the linear congruential generator, the constant multiplier generator, and the Fibonacci generator. You can ask Baidu for the principle, so I won't introduce it here, but only summarize the code implementation.


Square median generator

clc

clear all

close all


% Call function---parameters can be modified

[x0,u0] = Square_mid_rand(156, 2, 20);


% define function

function [xu] = Square_mid_rand(x0, k, maxIter)  

    x = []; % Initialize an empty matrix

    u = [];

    x(1) = x0; % Assignment operation, that is, x0 is placed in the first component of vector x

    u(1) = x(1) / 10^(2*k);

    for i = 2 : maxIter % loop

        x(i) = rem(floor(x(i-1)^2 / 10^k), 10^(2*k)); % Random number iteration generator

        u(i) = x(i) / 10^(2*k);

    end

    

    % Draw the image

    subplot(1,2,1);  

    hold on; % Maintain the current state.

    plot(u(3:end), '*-'); % The data is from the third component of u to the last component, * marks the key point

    sTitle = sprintf('Square middle random number generator (x0=%d, k=%d)', x0, k); % format the output string

    title(sTitle); 

    hold off;

    

    subplot(1,2,2); 

    hold on;

    hist(u(3:end));

    title('Random number histogram distribution');

    hold off;

end


Product Center Generator

clc

clear all

close all


% Call function---parameters can be modified

[x0, u0] = multiply_mid_rand(5167, 3729, 2, 20)

% define function       

function [xu] = multiply_mid_rand(x0, x1, k, maxIter)

    x = [];

    u = [];

    x(1) = x0; x(2) = x1;

    u(1) = x(1) / 10^(2*k);

    for i = 3 : maxIter

        x(i) = rem(floor(x(i-1) * x(i-2) / 10^k), 10^(2*k));

        u(i) = x(i) / 10^(2*k);

    end

    

    hold on;

    subplot(1,2,1);

    plot(u(3:end), '*-');

    sTitle = sprintf('Random number generator for product (x0=%d, x1=%d, k=%d)', x0, x1, k);

    title(sTitle);

    

    subplot(1,2,2);

    hist(u(3:end));

    title('Random number histogram distribution');

    hold off;

end


3. Linear congruential generator


clc

clear all

close all


% Call function---parameters can be modified

[x0,u0] = linear_mod_random(5, 5, 9, 5, 20)


% define function      

function [x,u] = linear_mod_random(a, c, m, x0, iterMax)

    x = [];

    u = [];

    x(1) = x0;

    u(1) = x(1) / m;

    for i = 2 : iterMax

        x(i) = a * x(i-1) + c;

        x(i) = mod(x(i), m);

        u(i) = x(i) / m;

    end

    

    hold on;

    subplot(1,2,1);

    plot(x(2:end), '*-');

    sTitle = sprintf('Linear congruential random number generator (a=%d, c=%d, m=%d, x0=%d)', a, c, m, x0);

    title(sTitle);    

    

    subplot(1,2,2);

    hist(u(2:end));

    title('Random number histogram distribution');

    hold off;

end


4. Constant multiplier generator


clc

clear all

close all


% Call function---parameters can be modified

[x0,u0] = con_mid_rand(6983, 2687, 2, 20);


% define function

function [xu] = con_mid_rand(x0, M, k, maxIter)

    x = [];

    u = [];

    x(1) = x0; 

    u(1) = x(1) / 10^(2*k);

    for i = 2 : maxIter

        x(i) = rem(floor(x(i-1) * M / 10^k), 10^(2*k));

        u(i) = x(i) / 10^(2*k);

    end

    

    hold on;

    subplot(1,2,1);

    plot(u(2:end), 'r*-');

    Title = sprintf('Constant multiplier random number generator (x0=%d, M=%d, k=%d)', x0, M, k);

    title(Title);

    

    subplot(1,2,2);

    hist(u(2:end));

    title('Random number histogram distribution');

    hold off;

end


5. Fibonacci Generator


clc

clear all

close all


% Call function---parameters can be modified

[x0,u0] = fib_rand(9, 20);


% define function

function [xu] = fib_rand(m, MaxIter)

    x = []; modX = [];

    u = [];

    x(1) = 1; x(2) = 1; 

    for i = 3 : MaxIter

        x(i) = x(i-1) + x(i-2);

        modX(i) = rem(x(i), m);

        u(i) = modX(i) / m;

    end

    

    hold on;

    subplot(1,2,1);

    plot(modX(3:end), 'r*-');

    sTitle = sprintf('Fibonacci random number generator (m=%d)',m);

    title(sTitle);

    

    subplot(1,2,2);

    hist(u(3:end));

    title('Random number histogram distribution');

    hold off;

end


The above are five random number generators simply implemented using MATLAB. In the future, we will add the performance test of the combination generator and the generated random number sequence.


Reference address:Five commonly used random number generators-MATLAB implementation

Previous article:Design and Implementation of Function Signal Generator
Next article:Function signal generator based on Multisim - square wave, triangle wave, sine wave

Recommended ReadingLatest update time:2024-11-16 22:48

Research on Power Factor Measurement Circuit of High Frequency Switching Power Supply Based on Matlab
0 Introduction The power factor of a high-frequency switching power supply is a very important parameter, which directly determines whether the product meets the general harmonic standards and measures the quality of the product. In order to reduce harmonics and improve the power factor, high-frequency
[Power Management]
Combining Matlab with LabVIEW
Starting from LabVIEW 8.0, LabVIEW provides a very useful function called Mathscript, which can be used to program using a text-based mathematical programming language similar to MATLAB. After the development of several versions 8.0, 8.2, 8.5, and 8.6, the functions of Mathscript have become more and more powerful. In
[Test Measurement]
Combining Matlab with LabVIEW
Latest Test Measurement Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号