Why do I get the same numbers in "randn" function?
古いコメントを表示
Every time I open MATLAB the first time I get the same values when I use randn function. This happens only after I first open MATLAB and not when the program is already open and I keep coming back for calculations.
1 件のコメント
Todd Flanagan
2011 年 1 月 24 日
Itamar says, "Thanks everyone for the answers. I learned something today"
採用された回答
その他の回答 (2 件)
Todd Flanagan
2011 年 1 月 24 日
The sequence of numbers produced by randn is determined by the internal state of the uniform pseudorandom number generator that underlies rand, randi, and randn. randn uses one or more uniform values from that default stream to generate each normal value. Control the default stream using its properties and methods. See @RandStream for details about the default stream.
Resetting the default stream to the same fixed state allows computations to be repeated. Setting the stream to different states leads to unique computations, however, it does not improve any statistical properties. Since the random number generator is initialized to the same state every time MATLAB software starts up, rand, randn, and randi will generate the same sequence of numbers in each session until the state is changed.
RandStream.setDefaultStream ...
(RandStream('mt19937ar','seed',sum(100*clock)));
randn(1,5)
1 件のコメント
Cris Luengo
2011 年 1 月 24 日
Todd beat me to it! :)
Cris Luengo
2011 年 1 月 24 日
All you need to do is read the documentation for the function randn: http://www.mathworks.com/help/techdoc/ref/randn.html
>> "Since the random number generator is initialized to the same state every time MATLAB software starts up, rand, randn, and randi will generate the same sequence of numbers in each session until the state is changed."
In the examples section on that page you see:
>> "Replace the default stream at MATLAB startup, using a stream whose seed is based on clock, so that randn will return different values in different MATLAB sessions. It is usually not desirable to do this more than once per MATLAB session."
And then the following example code:
RandStream.setDefaultStream ...
(RandStream('mt19937ar','seed',sum(100*clock)));
Cheers, Cris.
カテゴリ
ヘルプ センター および File Exchange で Random Number Generation についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!