Why does the random number generator in MATLAB fail a particular test of randomness?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 27 Jun. 2009
Bearbeitet: MathWorks Support Team
am 19 Apr. 2023
The RAND function in MATLAB is not random in generating numbers that are small. The generation of very small values following runs of not-small values is particularly low. Here is an example that illustrates the problem:
rand('state',0);
n = 5*10^7;
delta = .05;
runs = diff(find(rand(n,1)<delta))-1;
y = histc(runs, 0:100) ./ length(runs);
plot(0:100,(1-delta).^(0:100).*delta,'k--', 0:100,y,'b-');
title('Distribution of run lengths')
xlabel('Run Length'); ylabel('Relative Frequency');
legend({'Expected (Geometric) Distribution' 'Actual Distribution'})
This code plots the number of random numbers that must be generated until another very small random number is generated. From the plot we can see that there is a noticeable decrease in runs of length 27
Akzeptierte Antwort
MathWorks Support Team
am 18 Apr. 2023
Bearbeitet: MathWorks Support Team
am 19 Apr. 2023
This enhancement has been incorporated in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
This problem is due to some of the internal parameters used in the MATLAB random number generator. It is a shortcoming of the design of the algorithm, not a bug in the code. (As a note of interest, similar shortcomings are not uncommon in all pseudo-random number generators).
To work around this issue, you can download a different random number generator from the MATLAB Central File Exchange. It is called the Mersenne Twister, and is a relatively recent uniform random number generator algorithm. This is available at the following URL:
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!