Solving Fortran 77 Mystery: nran(i)=mod(int(i*ranw(Idum)),i) + 1

In summary, the line you are seeing in your code is a declaration of a function, and it is only accessible when it is called after the variable declarations. The line declares an array of size i for the function to use.
  • #1
kanato
415
1
I have this line in a Fortran 77 program, and I don't understand how it works:

nran(i)=mod(int(i*ranw(Idum)),i) + 1

What I don't understand is that nran is not a function, nor is it dimensioned anywhere (its type is implicitly an integer). This line only works if it comes right after the variable declarations in the function. If I put some non-declaration statement above it then the compiler (ifort 9.1, Linux ia64) complains that nran is not a function or dimensioned anywhere. That's sort of a big "duh" but I don't understand why this works if it's above with the variable declarations. It seems to be declaring an array for nran, but I have no idea how it knows what size/bounds to make it, as there is no indication of what values i should take.

If you are interested, this is a program based off the lisaqmc.f program available http://www.physics.rutgers.edu/~udo/qmc/readme_lisaqmc.html" .

(I should mention, I understand what mod and int are, and ranw is a function which returns a random real number using the seed given in Idum. This line would be totally clear to me if it was in a loop and nran was dimensioned somewhere.)
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Hi kanato,

That is a statement function. In fortran 77 rather than write a separate function subprogram, you could use statement functions, which were one-line declarations of functions that had to come before the executable statements (and that were only accessible to the program part that called them). You should be able to find instances where nran() is being called later on in that same part of the program.

So the line you are referring to is the definition of the function nran() that is being used elsewhere in the program.
 
  • #3
Ok, that makes a lot more sense. I was a little confused because it looks like it's being read as an array latter on, but of course reading from an array and calling a function have the same syntax. Thanks for the clarification.
 

Related to Solving Fortran 77 Mystery: nran(i)=mod(int(i*ranw(Idum)),i) + 1

1. What is Fortran 77?

Fortran 77 is a programming language commonly used in scientific and engineering applications. It was first released in 1977 and is an extension of the original Fortran language.

2. What does the code "nran(i)=mod(int(i*ranw(Idum)),i) + 1" mean?

This code is used to generate a random number between 1 and i, where i is a given integer. The variable Idum is a random number seed, while ranw is a function used to generate a random number. The mod and int functions are used to manipulate and restrict the range of the generated number.

3. What is the purpose of the code "nran(i)=mod(int(i*ranw(Idum)),i) + 1"?

The purpose of this code is to generate a random number within a specific range for use in a Fortran 77 program. This is commonly used for simulations and statistical analysis in scientific research.

4. How does the Fortran 77 "mod" function work?

The "mod" function in Fortran 77 returns the remainder after dividing the first argument by the second argument. In the code "nran(i)=mod(int(i*ranw(Idum)),i) + 1", it is used to restrict the range of the generated random number to be between 1 and i.

5. Why is the "nran" variable being assigned a random number instead of just using the "ranw" function?

The "nran" variable is being used to store the generated random number for future use in the program. This allows for the random number to be used multiple times without having to generate it again, which can improve the efficiency of the program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
722
  • Programming and Computer Science
Replies
13
Views
909
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top