Question

Is there a way to initialise a random array without using explicit do-loops?

Right now my random matrix initialisation looks like

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

But if possible I'd like it to be more along the lines of an implied do-loop array initialisation:

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

Is there any way something like this would be possible with Fortran 95?

Was it helpful?

Solution

Why not

call RANDOM_NUMBER(matrix) ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top