Why does the random number generator in MATLAB fail a particular test of randomness?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 27 Giu 2009
Modificato: MathWorks Support Team
il 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
Risposta accettata
MathWorks Support Team
il 18 Apr 2023
Modificato: MathWorks Support Team
il 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 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!