利用系统的random_number和random_seed函数生成随机实数,再在此基础上转换成整数,
子程序Randi可以自定义随机数的上下界。
Program main
implicit none
integer,allocatable::a(:)
integer n,i
integer min,max
write(*,*) "Input the length of Array:"
read(*,*) n
write(*,*) "Input the range of Array(Min,Max):"
read(*,*) min,max
allocate(a(n))
call RANDOM_SEED()
do i=1,n
call Randi(min,max,a(i))
enddo
write(*,*) "Array is created:"
do i=1,n
write(*,*) a(i)
enddo
deallocate(a)
stop
end
subroutine Randi(min,max,s)
implicit none
real x
integer min,max,s
call RANDOM_NUMBER(x)
s=floor(x*(max-min+1))+min
end