Clear Filters
Clear Filters

normrnd vs.random

20 views (last 30 days)
Andrea
Andrea on 20 Jun 2011
Dear, I would like to generate random nmbers with a normal distribution. From the documentation I see that there are two functioncs that seem to make the same things. normrnd(mu,sigma) and random('Normal',mu,sigma)
I wrote these two simpole for-loop in order to evaluate time consumption of these two functions and I found that normrnd is less time-consuming than random. I don't understand this difference. Could you help me?
tic for i=1:1000000 ciao=ciao+normrnd(0,0.01); end toc
tic for i=1:1000000 ciao=ciao+random('Normal',0,0.01); end toc
Kind regards
Andrea Giostri

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 20 Jun 2011
Hi,
if you open random with an editor and search for "normrnd", you will see, that random in fact calls normrnd. That's why it takes a little more time. On the other hand: if you call normrnd and random one time with N=1000000 instead of N calls in a loop, it won't make a difference at all ...
Titus
  4 Comments
Andrew Newell
Andrew Newell on 20 Jun 2011
Aaagh! Right at the top of http://www.mathworks.com/matlabcentral/answers/1759-dumb-mistakes-we-make-with-matlab! Thanks for reminding me.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics 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!