作业帮 > 综合 > 作业

用户自行输入起始值和终值,产生随机数的个数,fortran程序总是出错?

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/08 13:21:49
用户自行输入起始值和终值,产生随机数的个数,fortran程序总是出错?
生成指定两个数值间的多个随机数,随机数的个数用户自己输入,结果放在动态数组中
program main
implicit none
使用接口(interface)定义随机数范围
interface
function get_random(lbound,ubound)
real::lbound,ubound
real,allocatable::get_random(:) 返回值是数组类型
end function
end interface
定义产生结果的数组
real,allocatable::a(:)
定义产生随机数的个数
integer::size
定义随机数的值域范围
real::start_num,finish_num
用户自行输入随机数值域范围
write(*,*)"分别输入随机数的起始值和终值:"
read(*,*)start_num,finish_num
用户自行输入产生随机数的个数
write(*,*)"随机数的个数:"
read(*,*)size
动态分配存储结果的数组
allocate (a(size))
库存子程序,使用随机数之前调用
call random_seed()
a=get_random(start_num,finish_num,size)
write(*,*)a
end
子程序,用于生成随机数
function get_random(lround,uround,num)
real::lround,uround
\x05real,allocatable::get_random(:)
\x05real::t 产生0~1之间的随机数
\x05integer::num 产生随机数的个数
\x05real::length 所求随机数的长度
\x05integer::i 计数器
\x05allocate(get_random(num))
\x05length=uround-lround
\x05do i=1,num
\x05 call random_number(t) t是0~1之间的随机数
\x05 get_random(i)=lround+length*t
\x05end do
end
为什么运行时总有一个错误:f90:Fatal:There has been an internal compiler error (C0000005).
修改为interface
function get_random(lbound,ubound,num)
real::lbound,ubound
real,allocatable::get_random(:) 返回值是数组类型
integer:;num
end function
end interface
结果还是之前的错误
function里面不用动态数组就好了,你的size已经传递到num里面了,所以function直接声明:integer::numreal::get_random(num)
而integerface里面改一下就好了,另外动态数组是不是该释放的?deallocate?