Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29276 Discussions

Subroutine RANDOM returns 0<=ran<=1 and not 0<=ran<1?

andimb
Beginner
1,420 Views

Hi,

I tried to use the subroutine RANDOM of the IFORT-Compiler 10.0. The Language Reference said it would return a random number reater than or equal to
zero and less than one from the uniform distribution. But I also got the value 1.
Which Subroutine should I use with openmp. Cause RANDOM_NUMBER returns the same sequence in each thread.

Thanks

0 Kudos
7 Replies
TimP
Honored Contributor III
1,420 Views
You could use RANDOM_SEED in each parallel region to vary the starting point of RANDOM_NUMBER.
0 Kudos
Ron_Green
Moderator
1,420 Views
One other question I have is how you determined that you got an exact value of 1.0. Did you print the hex for the real value, did you write(*,*) or print*, did you print using a F format, or did you use a comparison of some sort IF ( x .eq. 1 ) THEN etc.

I would like to determine if there is a bug with our random_number, since the spec requires that it is strictly < 1
0 Kudos
andimb
Beginner
1,420 Views

Hi,

what I tried to do is to create an random integer with an upper bound which was used as an array index. I wrote a function like

REAL :: rr
CALL RANDOM(rr)
intValue = INT(rr * upperbound) + 1

But the intValue in some cases was one larger than the arraybounds. So I tried to find the error with:

PROGRAM test
 USE IFPORT

IMPLICIT NONE

REAL :: zufall


CALL SEED(1)

DO
CALL RANDOM(zufall)
IF(zufall .GE. 1.0) THEN
WRITE(*,*) 'jetzt', zufall
exit
END IF
END DO
END PROGRAM test

And there was an output with 1.000000 . Did I something wrong?

Andreas

0 Kudos
Steven_L_Intel1
Employee
1,420 Views
I suggest using RANDOM_NUMBER and call RANDOM_SEED with no arguments just once at the beginning of the program.

Writing the value using list-directed output may cause rounding As suggested, you want to write it in hex form, or perhaps to many digits (at least 10).
0 Kudos
Ron_Green
Moderator
1,420 Views
You probably discovered this, but just to document this thread for the next user who comes along:

RANDOM and RAND are functions from the Portability library, IFPORT. These return a number in the range 0.0 to 1.0 INCLUSIVE. So 1.0 is a valid return for RANDOM.

The Fortran 90 intrinsic, random_number, returns 0.0 <= number < 1.0, which is what you were looking for.

ron
0 Kudos
andimb
Beginner
1,420 Views
Hi,

just to get this right. There are two functions RANDOM and RAND from the IFPORT library that return 0<=value<=1.
But there is also a SUBROUTINE RANDOM from the IFPORT library that returns 0<=value<1 (Language Reference - Document Number: 253261-006US - page 1159) like the subroutine RANDOM_NUMBER. And I tried to use the subroutine that should not give a one.

So 1.0 is a valid return for the function RANDOM but not for the subroutine. Is this right or am I wrong?

Andreas
0 Kudos
Steven_L_Intel1
Employee
1,420 Views
The documentation supports your description. I don't entirely trust the description of the function RANDOM but it may be correct - I'll play with that and see. But I do strongly recommend using RANDOM_NUMBER instead.
0 Kudos
Reply