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

rand(0) produces NaN

mwpowel
Beginner
710 Views

We are in the process of porting over some code from Linux Fortran to 32-bit Windows Fortran, and we're at a point where we use the rand() functionality. We call rand(0), which we're expecting to produce a real number between 0.0 and 1.0. However, the result ends up being NaN. Still troubleshooting into this. What could be up with rand() that we get NaN? Do we need to install a library someplace to allow this to happen correctly?

-Michael

0 Kudos
4 Replies
mwpowel
Beginner
710 Views

More info... It looks like we're calling rand() something like this,

real :: temp
! ...
temp= rand(0)

Which returns NaN.

integer :: temp
! ...
temp= rand(0)

Which returns MIN_INT or something like, -2147483648.

This doesn't look quite right. Note this works on the Linux platform, not using the Intel compiler I gather (?).

Are we doing something wrong? Do we need to call it differently? Any ideas?

0 Kudos
Steven_L_Intel1
Employee
710 Views
What you describe should work as-is. Can you show us a small but complete program that demonstrates the problem? Have you declared a type for RAND? (You shouldn't).

You can try, as an experiment, adding:

use ifport

in the routine that calls rand. This is not required, but it might identify a coding error.
0 Kudos
mwpowel
Beginner
710 Views

Actually, we've decided to make use of random_number() instead of rand() as this better meets the business requirement.

FYI, for what it's worth, stepping into the rand() call, we end up in a C-style function, which accepts an integer and returns an integer. Not sure why this is or how it is possibly being mangled on the return to the Fortran caller, however.

As far as example code goes, I posted a snippet representing what code was being used to generate rand() values. It's pretty straightforward. That was it.

At any rate, a non issue as far as our code is concerned. random_number() and not rand() produces the results we're looking for.

-Michael

0 Kudos
Steven_L_Intel1
Employee
710 Views
The snippet was not enough to demonstrate the problem, but as I'd recommend random_number anyway, that's fine.
0 Kudos
Reply