フィルターのクリア

rand('seed',1)

43 ビュー (過去 30 日間)
Ricky
Ricky 2011 年 11 月 30 日
回答済み: ZHU CHENG-CHIH 2020 年 3 月 27 日
hi there, can anyone explain me the different between using 'rand('seed',1)' inside the loop and outside the loop? Assuming I generate some random number inside the loop.
Ta,Rak

採用された回答

Jan
Jan 2011 年 11 月 30 日
Seeding the random number generator means initializing it to a certain status. Seeding inside the loop means, that all "random" numbers created inside the loop will be the same in each iteration:
for i = 1:3
rand('seed', 1);
disp(rand);
end
Result: 0.51291 0.51291 0.51291
This is not very useful. Seeding RAND outside the loop allows you to reproduce the results:
for j = 1:2
rand('seed', 1);
for i = 1:3
disp(rand);
end
end
Now you get two series of 3 different numbers:
0.51291 0.46048 0.3504 0.51291 0.46048 0.3504
  3 件のコメント
Ricky
Ricky 2011 年 12 月 1 日
got it now, thanks alot
Peter Perkins
Peter Perkins 2011 年 12 月 1 日
Unless you are using an extraordinarily old (from the early 1990's) version of MATLAB, you should not be using rand('seed',1) at all. It most likely does not do what you think it does. If you are using R2011a or newer, see this:
<http://www.mathworks.com/help/techdoc/math/bs1qb_i.html>
Otherwise, see the documentation in whatever version you are using.

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

その他の回答 (1 件)

ZHU CHENG-CHIH
ZHU CHENG-CHIH 2020 年 3 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by