Do the RAND and RANDI functions sometimes produce non-uniform or zero-biased sequences in MATLAB 7.7 (R2008b)?
6 views (last 30 days)
Show older comments
The functions RAND and RANDI are supposed to produce sequences of random numbers that are uniformly distributed, but this sometimes does not appear to be the case. Take the following examples:
% Case 1: 10000 random integers in range [0,10]:
r = randi([0,10],10000,1);
hist(r)
There seems to be a heavy bias at zero in the resulting histogram. Here is a second example:
% Case 2: 10000 uniform random integers in range [0,15]
r = randi([0,15],10000,1);
hist(r)
The resulting histogram seems to show a complicated pattern of bias.
Accepted Answer
MathWorks Support Team
on 13 Oct 2009
These specific examples involving RANDI are actually deceiving. Although the sequences appear to be non-uniform, the RANDI function does produce uniformly distributed integers. The issue here is not the functionality of RANDI, but rather it is the usage of the function HIST.
When only one argument is passed to HIST, this function defaults to a histogram of 10 bins, regardless of the dynamic range of the input data. So for instance, if we execute the following code:
r = randi([0,10],10000,1);
hist(r)
Our data in "r" spans 0 to 10, so it has 11 "possibilities". When we generate a 10-bin histogram (by default), the first bin will actually contain all of the 0's in "r", as well as all of the 1's in "r". Thus, the first bin is roughly twice in size as compared to the rest of the bins. If we explicitly generate an 11-bin histogram, we see that there is no bias:
r = randi([0,10],10000,1);
hist(r,11)
0 Comments
More Answers (0)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!