MATLAB: Rand function is not uniform in large intervals

MATLABrand function

I am using rand function to generate uniformly distributed random numbers in the interval [10e-6 and 1.] But the function generates the nos. which are close to 1 (RATHER THAN BEING UNIFORM IN THE ENTIRE INTERVAL]. I have tried with 10 nos. and 100 nos. But I found that most the nos. generated are close to 1. Then, how it will be a uniform distribution??

Best Answer

Sigh. I think this is a misunderstanding of what uniformly random means. A fairly common one too. Since you seem not interested in showing your code, one can only guess the problem though. There can be no certain answer if I do not see your code. (addendum: The OP has since added a comment that indicates my conjecture is exactly on target.)
You are generating numbers uniformly random with a target interval [1e-6,1].
A uniform distribution implies that for ANY sub-interval of fixed width that is contained in the global window [1.e-6,1] (so assume a sub-interval [a,b]) where we have
1e-6 <= a <= b <=1
then the expected number of events we will observe should be:
(b - a)/(1 - 1e-6)
If you will generate N samples, then the expected number of events in the sub-interval is:
N*(b - a)/(1 - 1e-6)
So expect to see a number of events that are proportional to the sub-interval width. You won't seee exactly that many, since this is a random sampling.
So a uniform random sampling on the interval [0,1] would have roughly 10% of the samples in each bin [0,0.1], [0.1,0.2], [0.2,0.3], etc.
Now in your case, you are sampling on the interval [1e-6,1]. You find that very few samples occur right down at the bottom end, say between [1e-6,1e-5].
Lets use the rule above to see what fraction of the samples SHOULD occur in that interval. Lets say that we generate a sample size of 1000 values in the overall interval. Seems pretty big to me.
1000*(1e-5 - 1e-6)/(1 - 1e-6)
ans =
0.009
Hmm. I only expect to see 0.009 samples in that sub-interval, whereas I would have expected to see
1000*(1 - 0.9)/(1 - 1e-6)
ans =
100
So 100 events in the subinterval [0.9,1].
Is this truly uniform sampling? YES!!!!!!!!! Of course it is! You need to understand that the first interval I showed is a terribly tiny interval.
If you asked to generate a sampling that is uniformly probable over that region, but what you REALLY wanted was some sort of sampling that is uniform in a log space, then you needed to use a proper random sampling scheme!
For example, try this:
R = 10.^(rand(1,1000)*6 - 6);
Look at some percentiles of this sampling scheme:
Min 1.005e-06
1.0% 1.144e-06
5.0% 2.036e-06
10.0% 4.529e-06
25.0% 4.117e-05
50.0% 0.001265
75.0% 0.03185
90.0% 0.2554
95.0% 0.5309
99.0% 0.8607
Max 0.9928
It is NOT uniform, at least not in the domain [1e-6,1]. But the log10 of those numbers WILL be uniformly distributed. So, we will expect roughly 50% of the log10 values to be less than -3.
Min -5.998
1.0% -5.941
5.0% -5.691
10.0% -5.344
25.0% -4.385
50.0% -2.898
75.0% -1.497
90.0% -0.5928
95.0% -0.275
99.0% -0.06513
Max -0.003133
Again, it won't be perfect. But a sample size of 1000 is not really that huge. These predictions only become valid in the limit as N grows to a really large number.
Again, it is just a wild guess.