1. GFortran 的 seed 函数需要费点劲,你需要手动获得系统时间并当做种子。它的帮助里给了一个例子,可以复制下来以后备用。
2. 你的程序在我的 IVF for windows XE2013 SP1 上可以编译。不会有冲突。
3. 返回数组的话,通常用subroutine,避免使用function。如果非要用 function,可以封装到 module 里。
!// 后面是我的注释[Fortran] 纯文本查看 复制代码 program randomnum
implicit none
integer n
real l,u
real,allocatable::a(:)
call init_random_seed() !// gfortran 的seed需要更标准的写法
n=9 ;l=3.02;u= 311.0
allocate(a(n))
call get_random(n,l,u,a)
write(*,90) a
90 format(/,f7.3)
deallocate (a)
end program randomnum
!==================================================
Subroutine get_random(n,l,u,a) !// 一般返回数组的话,用 subroutine 会简单一些
implicit none
integer n,i
real l,u,length,t
real::a(n)
!l,u分别代表了数组的上下限
length=u-l
do i=1,n
call random_number(t)
a(i)=l+length*t
end do
end Subroutine get_random
!==================================================
Subroutine Init_random_seed() !// 这个函数你可以复制下来以后使用
integer :: i, n, clock
integer, dimension(:), allocatable :: seed
call random_seed(size = n)
allocate(seed(n))
call system_clock(count=clock)
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
call random_seed(put = seed)
deallocate(seed)
End Subroutine Init_random_seed
|