Explain this one line of code

8 ビュー (過去 30 日間)
Aftab Ahmed Khan
Aftab Ahmed Khan 2014 年 7 月 7 日
コメント済み: Star Strider 2014 年 7 月 7 日
Hi,
Can anyone explain to me this one line, what actually this one line do ?
rand('state',sum(100*clock)); % resetting of the random table

採用された回答

Star Strider
Star Strider 2014 年 7 月 7 日
It’s an obsolete way of resetting the seed of the random number generator. See the documentation on Replace Discouraged Syntaxes of rand and randn for details.
  2 件のコメント
Aftab Ahmed Khan
Aftab Ahmed Khan 2014 年 7 月 7 日
Hi Strider,
In short, does it mean that everytime i use the rand function, it will generate a different set of numbers? Am i right ?
Star Strider
Star Strider 2014 年 7 月 7 日
Yes. It’s set up in that statement to set the seed based on the current time (the sum of the elements in the six-element date vector returned by clock).

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

その他の回答 (2 件)

Ben11
Ben11 2014 年 7 月 7 日
I think this ensures that random numbers generated from different calls are all different since you use the current date/time in your call. You might want to take a look at this:
in which The Mathworks discourages using this syntax and proposes alternatives.

Shashank Prasanna
Shashank Prasanna 2014 年 7 月 7 日
編集済み: Shashank Prasanna 2014 年 7 月 7 日
Aftab, Random number generators are really pseudorandom (deterministic). This means for a given "seed" you can consistently regenerate the same sequence. This is useful if you want to re-run your experiment or perform multiple experiments on the same random stream.
In short, yes, using clock to reset the seed with generate a different stream each time since the clock is different each moment. However the recommended way to change the seed is to use the RNG function:
rng('shuffle')
Also all the link provided by SS and Ben are relevant.

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by