Re: 請問Fortran如何產生"不同"的隨機變數
在今年公告 (2007/06/05) 的 ISO1539 Fortran 標準規格中,並沒有說 Call RANDOM_SEED 會依據時間自動初始化。
可能是特定編譯器加進去的延伸功能。例如 IVF 線上手冊有提到有此功能,但不見得其他編譯器會有。
標準規格:
13.7.144 RANDOM SEED ([SIZE, PUT, GET])
1 Description. Restart or query the pseudorandom number generator used by RANDOM NUMBER.
2 Class. Subroutine.
3 Arguments. There shall either be exactly one or no arguments present.
SIZE (optional) shall be scalar and of type default integer. It is an INTENT (OUT) argument. It is
assigned the number N of integers that the processor uses to hold the value of the seed.
PUT (optional) shall be a default integer array of rank one and size N. It is an INTENT (IN)
argument. It is used in a processor-dependent manner to compute the seed value accessed
by the pseudorandom number generator.
GET (optional) shall be a default integer array of rank one and size N It is an INTENT (OUT)
argument. It is assigned the current value of the seed.
4 If no argument is present, the processor assigns a processor-dependent value to the seed.
5 The pseudorandom number generator used by RANDOM NUMBER maintains a seed that is updated
during the execution of RANDOM NUMBER and that may be specied or returned by RANDOM -
SEED. Computation of the seed from the argument PUT is performed in a processor-dependent manner.
The value returned by GET need not be the same as the value specied by PUT in an immediately
preceding reference to RANDOM SEED. For example, following execution of the statements
6 CALL RANDOM_SEED (PUT=SEED1)
CALL RANDOM_SEED (GET=SEED2)
7 SEED2 need not equal SEED1. When the values dier, the use of either value as the PUT argument
in a subsequent call to RANDOM SEED shall result in the same sequence of pseudorandom numbers
being generated. For example, after execution of the statements
8 CALL RANDOM_SEED (PUT=SEED1)
CALL RANDOM_SEED (GET=SEED2)
CALL RANDOM_NUMBER (X1)
CALL RANDOM_SEED (PUT=SEED2)
CALL RANDOM_NUMBER (X2)
9 X2 equals X1.
10 Examples.
11 CALL RANDOM_SEED ! Processor initialization
CALL RANDOM_SEED (SIZE = K) ! Puts size of seed in K
CALL RANDOM_SEED (PUT = SEED (1 : K)) ! Define seed
CALL RANDOM_SEED (GET = OLD (1 : K)) ! Read current seed
Intel Visual Fortran 9.1 有提到省略參數會用日期與時間進行初始化:
RANDOM_SEED
Intrinsic Subroutine: Changes or queries the seed (starting point) for the pseudorandom number generator used by RANDOM_NUMBER.
Syntax
CALL RANDOM_SEED ([size] [, put] [, get])
size
(Output; optional) Must be scalar and of type integer. Set to the number of integers (N) that the processor uses to hold the value of the seed.
put
(Input; optional) Must be an integer array of rank one and size greater than or equal to N. It is used to reset the value of the seed.
get
(Output; optional) Must be an integer array of rank one and size greater than or equal to N. It is set to the current value of the seed.
No more than one argument can be specified. If no argument is specified, a random number based on the date and time is assigned to the seed.
You can determine the size of the array the processor uses to store the seed by calling RANDOM_SEED with the size argument (see the second example below).
Examples
Consider the following:
CALL RANDOM_SEED ! Processor initializes the
! seed randomly from the date
! and time
CALL RANDOM_SEED (SIZE = M) ! Sets M to N
CALL RANDOM_SEED (PUT = SEED (1 : M)) ! Sets user seed
CALL RANDOM_SEED (GET = OLD (1 : M)) ! Reads current seed
The following shows another example:
INTEGER I
INTEGER, ALLOCATABLE :: new (:), old(:)
CALL RANDOM_SEED ( ) ! Processor reinitializes the seed
! randomly from the date and time
CALL RANDOM_SEED (SIZE = I) ! I is set to the size of
! the seed array
ALLOCATE (new(I))
ALLOCATE (old(I))
CALL RANDOM_SEED (GET=old(1:I)) ! Gets the current seed
WRITE(*,*) old
new = 5
CALL RANDOM_SEED (PUT=new(1:I)) ! Sets seed from array
! new
END
==> 本文由 "Ray <shockray.bbs@bbs.ccns.ncku.edu.tw>"
> 於 news:4WXU2R%24Jr7%40bbs.ccns.ncku.edu.tw 發表
> 感謝回答
> 但是我加入CALL RANDOM_SEED之後
> 每次執行出來的亂數還是一樣
> 我加入的地方是在random_number之前
> 例:
> call random_seed()
> call random_number(popu)
> Build程式之後,每次Excute出來的亂數都一樣
> 我問了google之後,找到關於random_seed的使用
> http://www.okok.org/cgi-bin/ut/topic_show.cgi?id=157388&h=1&bpg=1&age=30
> 他說: "random_seed()產生的種子是由當前計算机的日期和時間組成"
> 而且 "取決于中間語句的執行時間"
> 他加入了一個很長的迴圈,故意讓程式執行久一點
> do i=1, 1e10
> end do
> 之後再呼叫random_seed(),果然亂數就不一樣了
> 該不會Fortran每次想產生不同亂數都要這麼麻煩吧...??
>
> ※ 引述《devil@tainan.com.tw.x (璉璉)》之銘言:
> > 貼錯函數。
> > CALL RANDOM_NUMBER (harvest)
> > 這個是傳回亂數。
> > 純粹設亂數種子:
> > CALL RANDOM_SEED ([size] [, put] [, get])
> > ==> 本文由 "璉璉 <devil@tainan.com.tw>"
> > > 於 news:3CB93BFB9C0942BA9FABE2E445D16673%40c2e6400 發表
> > > Fortran 90 的標準應該是
> > > CALL RANDOM_NUMBER (harvest)
> > > SRAND 是 Intel Fortran 提供的延伸函式,並不被其他的 Fortran 編譯器支援。
>
--
風禹科技驗證有限公司 ASP.NET Web News Reader 0.2.7 UTF-8 Beta
網站地圖 http://tlcheng.twbbs.org/wwwmap.htm
流域防洪/區域水資源/徐昇網/玫瑰圖/語音通訊 文章與程式
Basic/Fortran/Windows API/.Net/輔助說明檔 原始碼、文章與討論
微軟程式設計、系統管理使用新技術論壇討論區,網友回覆後即時簡訊、電子郵件通知:
MSDN: http://forums.microsoft.com/msdn-cht/default.aspx?siteid=14
TechNet: http://forums.microsoft.com/technet-cht/default.aspx?siteid=23
--
ASPNET News Reader http://tlcheng.twbbs.org/News/Reader.aspx
RSS 2.0 http://tlcheng.twbbs.org/News/rss2.aspx?Action=List&Newsgroup=tw.bbs.comp.language
討論串 (同標題文章)
Programming 近期熱門文章
PTT數位生活區 即時熱門文章