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

7 ビュー (過去 30 日間)
Fred
Fred 2014 年 9 月 18 日
回答済み: Anne van Rossum 2016 年 6 月 29 日
Is there any difference between
rand(n,1)
and
unifrnd(0, 1, n, 1)?

採用された回答

Youssef  Khmou
Youssef Khmou 2014 年 9 月 18 日
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 件のコメント
Image Analyst
Image Analyst 2014 年 9 月 19 日
I find no unifrnd in base MATLAB, yet no toolbox is listed above.
Fred
Fred 2014 年 9 月 19 日
@Image Analyst It's in Statistics toolbox

サインインしてコメントする。

その他の回答 (1 件)

Anne van Rossum
Anne van Rossum 2016 年 6 月 29 日
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by