Why do I get the same numbers in "randn" function?

조회 수: 15 (최근 30일)
Itamar
Itamar 2011년 1월 24일
댓글: Steven Lord 2017년 4월 26일
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
Todd Flanagan 2011년 1월 24일
Itamar says, "Thanks everyone for the answers. I learned something today"

댓글을 달려면 로그인하십시오.

채택된 답변

Paulo Silva
Paulo Silva 2011년 1월 24일
You get the same numbers because there's a default seed (0) for the random number generator but you can change it like this:
RandStream.setDefaultStream(RandStream('mt19937ar','seed',sum(100*clock)))
If you want to see what changed do RandStream.getDefaultStream before and after changing the seed.
  댓글 수: 5
afef
afef 2017년 4월 26일
i tried this :
seed = sum(100*clock);
RandStream.setDefaultStream(RandStream('mt19937ar','seed',seed));
import java.util.Random;
import java.lang.System;
gen = Random(seed);
but i got error message "The class RandStream has no Constant property or Static method named 'setDefaultStream' " how to fix it?
Steven Lord
Steven Lord 2017년 4월 26일
We started announcing that you should replace calls to the setDefaultStream method for the RandStream class with calls to setGlobalStream in release R2011a, started warning users in release R2012a, and started having setDefaultStream throw an error in release R2013a.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Todd Flanagan
Todd Flanagan 2011년 1월 24일
This is by design:
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.
On that page there is an example of how to change the default stream of numbers using clock:
RandStream.setDefaultStream ...
(RandStream('mt19937ar','seed',sum(100*clock)));
randn(1,5)

Cris Luengo
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.

카테고리

Help CenterFile Exchange에서 Time Series Collections에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by