有没有一种方法可以在不使用显式 do 循环的情况下初始化随机数组?
现在我的随机矩阵初始化看起来像
program Main
implicit none
save
integer :: seed, i, j
real :: x
character(100) :: option
real, dimension(10,10) :: matrix
if (iargc() > 0) then
CALL GETARG(1,option)
read(option,*) seed
else
seed = 1
end if
call RANDOM_SEED(seed)
do i=1,10
do j=1,10
call RANDOM_NUMBER(x)
matrix(i,j) = x
end do
end do
end program
但如果可能的话,我希望它更符合隐含的 do-loop 数组初始化:
program Main
implicit none
save
integer :: seed
character(100) :: option
real, dimension(10,10) :: matrix
if (iargc() > 0) then
CALL GETARG(1,option)
read(option,*) seed
else
seed = 5
end if
call RANDOM_SEED(seed)
matrix = reshape((/ ((call RANDOM_NUMBER()), i=1,100) /), shape(matrix))
end program
有没有办法用 Fortran 95 做这样的事情?
最佳答案
为什么不
调用 RANDOM_NUMBER(matrix)
?
关于Fortran 中的随机数组/矩阵初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22636532/