How do I create a random number generator using congruent method

4 visualizzazioni (ultimi 30 giorni)
I want to create a random number generator that uses a congruent method. And I need to create a histogram that should have 0.2 for 1, 0.4 for 2 and 0.4 and 3. When I use rand the histogram works right.
The code I used was this:
prod_total = [];
for j = 1:1000
x0 = 4;
m = 5;
vetor_aleatorio = [ ];
for k = 1:100
[u1, x0] = n_aleatorio(x0, k, m);
vetor_aleatorio(k) = u1;
end
x = rand;
if (x <= 0.2)
prod = 1;
elseif (x > 0.2 & x <= 0.6)
prod = 2;
elseif (x > 0.6 & x <= 1)
prod = 3;
end
prod_total = [prod_total, prod];
end
histogram(prod_total)

Risposta accettata

Jan
Jan il 7 Mar 2023
Modificato: Jan il 7 Mar 2023
A very cheap linear congruential RNG with parameters suggested by Knuth:
function a = rng_cheap
% On-line Numerical Recipes in C
% http://lib-www.lanl.gov/numerical/bookcpdf.html
% Knuth suggests a = 1664525 as a suitable multiplier for this value of m. H.W. Lewis
% has conducted extensive tests of this value of a with c = 1013904223, which is a prime close
% to m*sqrt(5)/2. The resulting in-line generator (we will call it ranqd1) is simply
%
% unsigned long idum;
% idum = 1664525L*idum + 1013904223L;
%
% This is about as good as any 32-bit linear congruential generator, entirely adequate for many
% uses. And, with only a single multiply and add, it is very fast.
% To check whether your machine has the desired integer properties, see if you can
% generate the following sequence of 32-bit values (given here in hex): 00000000,
% 3C6EF35F, 47502932, D1CCF6E9, AAF95334, 6252E503, 9F2EC686, 57FE6C2D,
% A3D95FA8, 81FDBEE7, 94F0AF1A, CBF633B1.
persistent idum
if isempty(idum)
idum = 0;
end
idum = rem(1664525 * idum + 1013904223, 4294967296);
a = idum; % 32 bit integer 0:2^32-1
% a = idum / 4294967295; % Double with 32 bit resolution, [0,1]
% a = idum / 4294967296; % Double with 32 bit resolution, [0,1)
end
  2 Commenti
Pedro Almeida
Pedro Almeida il 8 Mar 2023
just a question, this follows a uniform distribution right?
Jan
Jan il 8 Mar 2023
I've included a link as source of this code. You can find the details there. I do not dare to comment comments of Donald E. Knuth.

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 7 Mar 2023
If this is not for a homework assignment, either use one of the existing random number generators in MATLAB (as listed on this documentation page the 'mcg16807' generator is a multiplicative congruential generator, though it has a very short period) via the rng function or use discretize to convert the uniform numbers from a call to rand into your desired distribution.
weights = [0.2, 0.4, 0.4];
breakpoints = cumsum([0, weights]);
breakpoints(end) = 1;
uniformData = rand(1, 1e6);
discretizedData = discretize(uniformData, breakpoints);
histogram(discretizedData, Normalization="probability");
yline([0.2 0.4], 'r:')
Those look in pretty good agreement with the weights vector.

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by