» 首页 » 电脑_数码 » 编程 » 在fortran中rand()的用法是怎样的?麻烦举个例子

在fortran中rand()的用法是怎样的?麻烦举个例子



rand()不大好用,用这个:
0-1之间类似于

program test
implicit none
real :: x
call random_seed()! 系统根据日期和时间随机地提供种子"
call random_number(x)
print *,x
end program


另外,在一个程序中, "call random_seed () 这一句是不能重复的,只能用一次。
所以它不能放到循环里面去,另外,如果一个子程序会被调用多次的话,也不能放到子程序里面。

==============
如果你非得用rand()的话,看看这个帖子吧:
http://bbs.pfan.cn/post-276179.html

rand()不大好用,用这个:
0-1之间类似于

program test
implicit none
real :: x
call random_seed()! 系统根据日期和时间随机地提供种子"
call random_number(x)
print *,x
end program


另外,在一个程序中, "call random_seed () 这一句是不能重复的,只能用一次。
所以它不能放到循环里面去,另外,如果一个子程序会被调用多次的话,也不能放到子程序里面。

==============
如果你非得用rand()的话,看看这个帖子吧:
http://bbs.pfan.cn/post-276179.html

用random_number
照下面的代码,rnd就是生成的随机数,大于0小于1。请注意,作为计算机来说,对任何语言,生成的随机数都不是真正随机的,近似而已。
INTEGER iseed
REAL*8 rnd
iseed = 425001

rnd = RAN(iseed)
c--
c rand.f: A sample program to generate a pseudorandom number sequence.
c This program prints ten numbers between 0.0 and 1.0, and it also
c prints a different sequence each time (unless you run it a LOT of
c times---the rand() functon is, after all, only pseudorandom).
c--
program rand
real rand ! Declare the type of the rand() function
integer i ! Counts random numbers
integer*4 timeArray(3) ! Holds the hour, minute, and second
c--
c In order to get a different sequence each time, we initialize the
c seed of the random number function with the sum of the current
c hour, minute, and second.
c--
call itime(timeArray) ! Get the current time
i = rand ( timeArray(1)+timeArray(2)+timeArray(3) )
c--
c Calling rand() with an argument of zero generates the next number
c in sequence.
c--
do i = 1, 10
print *, rand(0)
end do
stop
end


把它作为一个子程序
挺好用的
我经常用它

 相关问题
·在fortran中rand()的用法是怎样的?麻烦举个例子
·一、以下程序包含三个函数:sort,print,printfil,按照要...
·求一个批处理~!!
·求简单的php通用后台程序?
·在Textl中输入的任何字符,立即显示在Text2中。
·编写一类似QQ群聊的多人聊天程序
·请教这段是什么意思.
·关于学习asp.net
·pascal题目:机器人
·什么是动漫知识与动漫语言?
·输入2个字符串,输出较大的一个
·急!!!根据用户的ID调数据库中的内容显示
·判断字符个数及读取字段程序
·PHP中的问题
·求vbs 常用运算符,函数

 《在fortran中rand()的用法是怎样的?麻烦举个例子》答案收集时间:2008-06-14 14:28:16



©2007 电脑技术问答录