how rng and randn works in Matlab?

조회 수: 5 (최근 30일)
Weicheng Xue
Weicheng Xue 2019년 7월 25일
답변: Steven Lord 2019년 7월 26일
I know rng in matlab is used to control random number generation. However, I am not sure of how it works. A professor wants me to figure it out. I have three sets of questions. First, using h=rng(0) returns a structured type h, and h.State has a sequence of numbers (625). What are these 625 numbers used for? Second, if the 625 numbers is a psudonumber stream, and if I use randn to generate a sequence of data from normal distribution, based on this stream (if I am correct), then what if the number of Guassian samples required is greater than 625 (e.g. using randn(n,1), and n > 625). Will the samples be repeated then? Finally, if I call randn twice, will the sequence of samples for the second time the same as that for the first time, or is it a new sequence of data, i.e., a continuation of the previously generated sequence of samples?
I hope there are someone who could help me to figure this out. Thank you very much!
Best Regards,
Weicheng Xue

채택된 답변

Steven Lord
Steven Lord 2019년 7월 26일
See this post on Cleve's blog and this post on Loren's blog for more information on the random number generators and rng in MATLAB. [The second and third part of the series on Cleve's blog are the next two posts.]

추가 답변 (2개)

amin ya
amin ya 2019년 7월 25일
Use the followings to see the algorithm:
open rng
open randn
  댓글 수: 1
Weicheng Xue
Weicheng Xue 2019년 7월 25일
Ok. I am still not sure why state has 625 elements. The manual still does not say bout that. Thank you!

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


Walter Roberson
Walter Roberson 2019년 7월 25일
The choice of 624 as the state vector has to do with mathematical properties that hold when a certain quantity related to the size of the state vector turns out to be a Mersenne Prime; it has to do with showing that,
"Then each of the possible combinations of bits occurs the same number of times in a period, except for the all-zero combination that occurs once less often."
This in turn has to do with showing that if you take consecutive values as coordinates in any dimension up to 623, then the points will fill the space evening instead of showing lines of values -- lines of values is a nasty problem in the older random number technique of Linear Congruential pseudo-random generation, showing up even in just plotting pairs of adjacent generated values.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by