Technical Solutions
Why does the random number generator in MATLAB fail a particular test of randomness?
Date Last Modified: Friday, June 26, 2009
| Solution ID: | 1-10HYAS | |
| Product: | MATLAB | |
| Reported in Release: | R14SP1 | |
| Fixed in Release: | R14SP3 | |
| Platform: | All Platforms | |
| Operating System: | All OS |
Subject:
Why does the random number generator in MATLAB fail a particular test of randomness?
Problem Description:
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);
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
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'})
Solution:This enhancement has been incorporated in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds: |
|
|
Store

