在fortran中rand()的用法是怎样的?麻烦举个例子
4个回答
2008-05-24
展开全部
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
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
展开全部
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
把它作为一个子程序
挺好用的
我经常用它
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
把它作为一个子程序
挺好用的
我经常用它
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
照下面的代码,rnd就是生成的随机数,大于0小于1。请注意,作为计算机来说,对任何语言,生成的随机数都不是真正随机的,近似而已。
INTEGER iseed
REAL*8 rnd
iseed = 425001
rnd = RAN(iseed)
INTEGER iseed
REAL*8 rnd
iseed = 425001
rnd = RAN(iseed)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询