gfortran and random numbers

前端 未结 1 822
感情败类
感情败类 2020-12-18 07:59

I am trying to compile the following simple code using Gfortran 4.7 from mac-ports (OS-X):

program main

implicit none

integer :: n = 1, clock, i

integer,          


        
相关标签:
1条回答
  • 2020-12-18 08:22

    To amplify somewhat on @Yossarian's comment, this

    call random_seed(size = n)
    

    returns, in n, the size of the rank 1 integer array that you have to use if you want to initialise the RNG. I'd suggest making iseed allocatable by changing its declaration to:

    integer, dimension(:), allocatable :: iseed
    

    then, after getting a value for n, allocate it:

    allocate(iseed(n))
    

    populate it with your favourite values, then put it.

    You might be able to allocate and populate it in one statement like this:

    allocate(iseed(n), source = clock + 37 * [(i, i = 0,n-1)])
    

    I write might because this depends on how up to date your compiler is.

    EDIT, after OP comment

    No, you have not quite understood what I suggested.

    Get a value for n by executing

    call random_seed(size = n)
    

    don't initialise n to 12.

    Then allocate the array and populate it, either in one statement (using sourced allocation) or an allocate statement followed by an assignment.

    In

    allocate(iseed(n))
    call random_seed(size = n)
    

    the sequence of operations is incorrect. This sets iseed to have 12 elements (which is the value of n when the first statement is executed), and then sets n to the size of the array required by the RNG. So long as that is 12 you won't see any problems, but as soon as you port your code to another compiler, possibly even another version of the same compiler, you risk running into an RNG which requires an integer array of a different size. There is no need to hardwire a value into your code, so don't.

    0 讨论(0)
提交回复
热议问题