Fortran Coder

查看: 25650|回复: 10
打印 上一主题 下一主题

[求助] 随机数组 & 接口冲突

[复制链接]

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
608 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

楼主
发表于 2014-4-4 18:06:07 | 显示全部楼层
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

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
608 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

沙发
发表于 2014-4-4 18:40:33 | 显示全部楼层
hanshikai 发表于 2014-4-4 18:13
谢谢你指导性的意见【返回数组的话,通常用subroutine,避免使用function。如果非要用 function,可以封 ...

是的,不能。

你把一切都否定了,自然就不能了。

不要太介意 subroutine 和 function,他俩的差别很小。

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
608 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

板凳
发表于 2014-4-4 18:51:20 | 显示全部楼层
hanshikai 发表于 2014-4-4 18:49
谢谢。
结构体是什么? 英文?

C 里面叫 struct
Fortran 里面叫 type(中文通常叫派生类型)

其实两者是一回事。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-19 09:04

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表