看板Programming
标 题Re: 请问Fortran如何产生"不同"的随机变数
发信站=?big5?B?SElTRFQgrbes6qzsp97F58PSprOtraS9pX (Sat Oct 20 07:26:36 2007)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.ntu!feeder.seed.net.tw!netnews!
在今年公告 (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 <[email protected]>"
> 於 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每次想产生不同乱数都要这麽麻烦吧...??
>
> ※ 引述《[email protected] (琏琏)》之铭言:
> > 贴错函数。
> > CALL RANDOM_NUMBER (harvest)
> > 这个是传回乱数。
> > 纯粹设乱数种子:
> > CALL RANDOM_SEED ([size] [, put] [, get])
> > ==> 本文由 "琏琏 <[email protected]>"
> > > 於 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