MATLAB Answers

Is there any difference between rand(n,1) and unifrnd(0, 1, n, 1)?

93 views (last 30 days)
Fred
Fred on 18 Sep 2014
Answered: Anne van Rossum on 29 Jun 2016
Is there any difference between
rand(n,1)
and
unifrnd(0, 1, n, 1)?

  0 Comments

Sign in to comment.

Accepted Answer

Youssef  Khmou
Youssef Khmou on 18 Sep 2014
They both generate sample from uniform distribution, unifrnd offers a possibility to enter the parameters while with rand you need to adjust them, example of uniform distribution of interval [5,10] with size of 400 :
h1=unifrnd(5,10,1,400);
h2=5+5*rand(1,400); % same pdf

  3 Comments

John D'Errico
John D'Errico on 19 Sep 2014
In fact, unifrnd will call rand and do the transformation for you. In this specific case though, there is no transformation needed.
r = mu + sig .* (2*rand(sizeOut)-1);
Image Analyst
Image Analyst on 19 Sep 2014
I find no unifrnd in base MATLAB, yet no toolbox is listed above.

Sign in to comment.

More Answers (1)

Anne van Rossum
Anne van Rossum on 29 Jun 2016
There is less error checking in rand, which can be at times preferred... For example, if you need to generated random numbers between a and b, and it doesn't matter if a > b.
a = 5
b = -5
Then:
a+(b-a)*rand(1,400)
But you'll need to make sure a and b are properly ordered for unifrnd:
if (b < a)
[a, b] = deal(b, a);
end
unifrnd(a,b,1,400)
Or else your result will be a vector of NaNs.

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by