- The random function is a switchyard mostly intended to make it easy for a newcomer to find all the possible distributions in one place. If you're writing code that is time critical, you'll want to use the individual random number generator functions like unifrnd.
- If at all possible, you want to make vectorized calls to random, or even unifrnd, rather than getting one value at a time.
- random, and to a lesser extent even unifrnd, has error checking and some generality built into them. If performance is really critical, then you might consider using the example from the rand reference page:
Performance degradation of random number generation in MATLAB r2013a
5 views (last 30 days)
Show older comments
In MATLAB 2013a, the function random(...) from the Statistics Toolbox runs 40-50 times slower than the same function in the version 2012b.
Ratios of execution times of the following generators:
(a) random('unif', 0,1) and (b) unifrnd(0,1)
are 110 in MATLAB 2013a and 3.1 in MATLAB 2012b.
That is, the function random(...) is 3x slower than unifrnd(...) in MATLAB 2012b, but the same slowdown is 110x in the new MATLAB 2013a.
Any suggestions how to get the new version to perform as quickly as the previous one? (It would be better to have improved performance in the new version, indeed. ;) )
Below is the script used to measure execution times:
N = 5000;
% (a)
tic;
sm = 0;
for i=1:N
sm = sm + random('unif', 0, 1);
end
toc;
% (b)
tic;
sm = 0;
for i=1:N
sm = sm + unifrnd(0, 1);
end
toc;
0 Comments
Answers (2)
Peter Perkins
on 18 Mar 2013
Alexander, I will make a note to look into the difference in performance, but a couple of things you might keep in mind (you may already know these, but just in case):
Example 1: Generate values from the uniform distribution on the
interval [a, b].
r = a + (b-a).*rand(100,1);
Jan
on 19 Mar 2013
More a comment than an answer:
The random streams objects became more and more complicated since Matlab 5.3. When I e.g. want to get a random array controlled by a seed, I have to look in the documentation each time.
Compare:
rand('seed', now)
with:
s = RandStream('mt19937ar', 'Seed', now);
RandStream.setGlobalStream(s);
I understand, that the new random objects are much more powerful. But simple tasks are looking such ugly yet, that I prefer my own random toolbox - although this is known as a bad programming pattern and too many programs suffered from not relying on heavily tested RNGs.
One problem is, that the symbols "rand", "rng", "random" and "RandStream" must be avoided to keep the backward compatibility. Therefore a cleaner and more convenient new tool or a wrapper needs a not matching name already.
Some measurements with the Mersenne Twister (in R2009a and 2011b) look like the standard implementation is used, while there are some SSE-versions available already, which are much faster.
My conclusion: Matlab's RNG tools ran out of control.
5 Comments
Yoichi Watanabe
on 3 Apr 2013
Tom, I would appreciate your thorough explanation on the matter. In fact I used unifrnd instead of random and it runs as fast as the random on 2012b. Perhaps "random" should be removed since it runs so slow or one note should be added in the help for that function. That may be what you write here. Again thank you very much for your quick and adequate reply. Yoichi Watanabe
See Also
Categories
Find more on Random Number Generation 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!