用户自行输入起始值和终值,产生随机数的个数,fortran程序总是出错?生成指定两个数值间的多个随机数,随机数的个数用户自己输入,结果放在动态数组中program mainimplicit none使用接口(interface)
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/20 12:38:05
![用户自行输入起始值和终值,产生随机数的个数,fortran程序总是出错?生成指定两个数值间的多个随机数,随机数的个数用户自己输入,结果放在动态数组中program mainimplicit none使用接口(interface)](/uploads/image/z/12503398-22-8.jpg?t=%E7%94%A8%E6%88%B7%E8%87%AA%E8%A1%8C%E8%BE%93%E5%85%A5%E8%B5%B7%E5%A7%8B%E5%80%BC%E5%92%8C%E7%BB%88%E5%80%BC%2C%E4%BA%A7%E7%94%9F%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%9A%84%E4%B8%AA%E6%95%B0%2Cfortran%E7%A8%8B%E5%BA%8F%E6%80%BB%E6%98%AF%E5%87%BA%E9%94%99%3F%E7%94%9F%E6%88%90%E6%8C%87%E5%AE%9A%E4%B8%A4%E4%B8%AA%E6%95%B0%E5%80%BC%E9%97%B4%E7%9A%84%E5%A4%9A%E4%B8%AA%E9%9A%8F%E6%9C%BA%E6%95%B0%2C%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%9A%84%E4%B8%AA%E6%95%B0%E7%94%A8%E6%88%B7%E8%87%AA%E5%B7%B1%E8%BE%93%E5%85%A5%2C%E7%BB%93%E6%9E%9C%E6%94%BE%E5%9C%A8%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84%E4%B8%ADprogram+mainimplicit+none%E4%BD%BF%E7%94%A8%E6%8E%A5%E5%8F%A3%EF%BC%88interface%EF%BC%89)
用户自行输入起始值和终值,产生随机数的个数,fortran程序总是出错?生成指定两个数值间的多个随机数,随机数的个数用户自己输入,结果放在动态数组中program mainimplicit none使用接口(interface)
用户自行输入起始值和终值,产生随机数的个数,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
结果还是之前的错误
用户自行输入起始值和终值,产生随机数的个数,fortran程序总是出错?生成指定两个数值间的多个随机数,随机数的个数用户自己输入,结果放在动态数组中program mainimplicit none使用接口(interface)
function里面不用动态数组就好了,你的size已经传递到num里面了,所以function直接声明:
integer::num
real::get_random(num)
而integerface里面改一下就好了,另外动态数组是不是该释放的?deallocate?