View previous topic :: View next topic 
Author 
Message 
Kenneth_Smith
Joined: 18 May 2012 Posts: 177 Location: Glasgow, Scotland.

Posted: Tue Sep 05, 2017 9:32 pm Post subject: Random@ 


The following code is confusing me.
When compiled with checkmate win 32, or checkmate win 64 the values returned for x(1) and x(2) are identical.
When compiled with debug win 32, debug win 64, release 32 or release 64 the values for x(1) and x(2) are different.
Which behaviour is correct?
Code:  program test
implicit none
real(kind=2) random@
real(kind=2) x1(1:2)
x1 = 0.d0
x1 = RANDOM@()
write(6,*) x1
end program test 
Thanks
Ken 

Back to top 


JohnCampbell
Joined: 16 Feb 2006 Posts: 1968 Location: Sydney

Posted: Wed Sep 06, 2017 5:22 am Post subject: 


I confirmed your results.
The /check response looks wrong to me, although is it ?
ARRAY = scalar function or
ARRAY = elemental function ( if this is possible ?)
What would an ELEMENTAL FUNCTION do with an array argument ?
I used the following in PLATO with different options and got the different response for x1 = RANDOM@()
Code:  program test
implicit none
real(kind=2) random@
real(kind=2) x1(1:2), y1(1:2), z1(1:2)
integer i
!
x1 = 0.d0
x1 = RANDOM@()
do i = 1,size(y1)
y1(i) = RANDOM@()
end do
call random_number (z1)
write(*,*) x1
write(*,*) y1
write(*,*) z1
end program test 
I then tried another test on your code
Code:  program test
implicit none
integer*4, parameter :: n=2 ! try 2 or 3
real(kind=2) random@
real(kind=2) x1(1:n)
x1 = 0.d0
x1 = RANDOM@()
write(*,*) x1
end program test 
n=2 reproduces your experience, but n=3 produces the same value for all of x1 for all compile options; /check, /debug or none (release).
What is the correct result now ? 

Back to top 


Kenneth_Smith
Joined: 18 May 2012 Posts: 177 Location: Glasgow, Scotland.

Posted: Wed Sep 06, 2017 7:36 am Post subject: 


Thanks John,
My first thought was that the /debug was incorrect, but then I had some doubts about that. Your modified code has confused me even further.
It will be interesting to read what Paul makes of this.
Ken 

Back to top 


PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 5441 Location: Salford, UK

Posted: Wed Sep 06, 2017 7:36 am Post subject: 


Yes I agree. The results are not consistent and ought to be.
I will log that this needs fixing.
When /CHECK is applied there is only one call to RANDOM@ and this is incorrect.
For the time being you will need to use an explicit do loop....
Code:  do i=1,2
x1(i) = RANDOM@()
enddo



Back to top 


JohnCampbell
Joined: 16 Feb 2006 Posts: 1968 Location: Sydney

Posted: Wed Sep 06, 2017 7:57 am Post subject: 


Ken,
Using the Fortran intrinsic "call random_number (x1)" would also give the answer you are expecting, although I do prefer a DO loop approach (using the Fortran intrinsic).
John 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 934

Posted: Wed Sep 06, 2017 2:36 pm Post subject: 


I think that FTN95/64 has an incorrect notion of the type of the return value of the builtin nonstandard function RANDOM@(). The test code, ks.f90,
Code:  program test
implicit none
double precision random@
write(*,'(ES12.4)')random@()
end program test

when run with ftn95 /64 /lgo ks.f90, gives the correct result, 6.7307E01. With the type declaration of random@ commented out, the output is 1.0025E+26. 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 934

Posted: Wed Sep 06, 2017 2:51 pm Post subject: Re: 


PaulLaidler wrote: 
When /CHECK is applied there is only one call to RANDOM@ and this is incorrect.

Here is a counterargument: RANDOM@ is a scalar, nondeterministic function with no arguments. Therefore, the expression in the second assignment statement in the program of the original post is a scalar, and the statement has the form <vector variable> = <scalar expression>. For such an assignment, Fortran requires that the expression should be evaluated only once, and the value copied into every element of the vector variable.
Is this argument incorrect? In what way? 

Back to top 


Kenneth_Smith
Joined: 18 May 2012 Posts: 177 Location: Glasgow, Scotland.

Posted: Wed Sep 06, 2017 8:09 pm Post subject: 


I do seem to have opened up a can of worms here. For 3 decades I have always used (as John suggested) a DO loop for this type of assignment, but I am trying to move with the times. My initial thought was that random@ should be called multiple times  perhaps due to my ingrained serial approach/thinking and this was confirmed by Paul, however Mecej's observation about vector = scalar seems entirely reasonable. I'm still confused as to what is formally "correct", but know how to overcome the problem. One for us all to be wary of in the future.
Thank you all for your input
Ken 

Back to top 


mecej4
Joined: 31 Oct 2006 Posts: 934

Posted: Wed Sep 06, 2017 11:39 pm Post subject: 


Although
double precision x1(10)
...
x1 = RANDOM@()
...
is syntatically correct, I cannot think of an application where it makes sense. Why would I want to generate a single random number and copy it into an entire array, in any Monte Carlo simulation?
We do need to get the compiler bugs (if any) fixed; as far as your program is concerned, however, I do not think that you should set any array variable equal to the result of RANDOM@() at all. 

Back to top 


