Fortran variable value changing randomly

In summary, the conversation discusses the use of a template and the value of a variable 'seed' in two different locations. It is determined that the value is being modified by the function congmult, which is passed the variable by reference. This modification is important to keep track of the sequence in the RNG.
  • #1
cpburris
Gold Member
38
4
This isn't really a question for which the template is useful..
Can anyone explain to me why the value of the variable 'seed' in location 1 is different from the value in location 2? The only thing happening in between is the defining of the varaible v(1) using the real function. I don't see where in that 'seed' is given a new value. I've tried google searching 'Fortran seed' and such and can't find any answer.
Code:
      JRDM = 83721563
      call baysprep(JRDM)
! -----------------------------------------
      subroutine baysprep(first)
          integer first,seed
          common /tobays/ v(53),oldbays,seed
          save /tobays/
          seed = abs(first)
       if (seed.gt.2147483647) seed = 24287
 
       do i = 1,53
!Location 1
        print*,'seed into do loop iteration=',seed
        v(1) = congmult(seed)
!Location 2
        print*,'seed coming out of do loop iteration=',seed
      end do
!
 real function congmult(ix)
!
       integer a,p,ix,b15,b16,xhi,xalo,leftlo,fhi,k
       data a,b15,b16,p/16807,32768,65536,2147483647/
      ! assigns variables these values
       xhi = ix / b16
       xalo = a*(ix - xhi*b16)
       leftlo = xalo / b16
       fhi = xhi*a + leftlo
       k = fhi / b15
       ix = (((xalo-leftlo*b16) - p) + (fhi-k*b15)*b16) + k
       if (ix.lt.0) ix = ix + p
       congmult = real(ix)/2.147483647e+09
       end
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
cpburris said:
This isn't really a question for which the template is useful..
Hi cpburris. Please use the template for posts to the homework forums regardless of whether you believe it is "suitable" or not. It's just one of those rules we're picky about :smile:

Check to see if your function parameters are call-by-reference or call-by-value. I see that seed is passed to function congmult which takes takes parameter ix and modifies it, so try printing out its value from inside that function just before it returns. See if the value shown matches that of 'seed coming out of do loop iteration'.
 
  • #3
gneill said:
Hi cpburris. Please use the template for posts to the homework forums regardless of whether you believe it is "suitable" or not. It's just one of those rules we're picky about :smile:

Check to see if your function parameters are call-by-reference or call-by-value. I see that seed is passed to function congmult which takes takes parameter ix and modifies it, so try printing out its value from inside that function just before it returns. See if the value shown matches that of 'seed coming out of do loop iteration'.

Ah, yes you were correct. The modification of the value by the function was being passed back. Thank you very much.
 
  • #4
You'e welcome. Glad I could help.
 
  • #5
gneill said:
Check to see if your function parameters are call-by-reference or call-by-value.
The latter doesn't exist in Fortran. All variables are passed by reference.

@cpburris: Note that this is how this RNG keeps track of where it is in the sequence. You give it an initial seed, but then it modifies it every time a new random number is generated. It is important not to change the value of seed between calls to congmult.
 
  • #6
DrClaude said:
The latter doesn't exist in Fortran. All variables are passed by reference.
It's something that programmers need to be aware of for whatever language they are using. Discovering the hard way is one path to enlightenment :smile:
 

1. Why is my Fortran variable value changing randomly?

There could be several reasons for this. One possibility is that your code contains a bug or logical error that is causing unexpected changes to the variable. Another possibility is that the variable is being affected by external factors, such as other processes or input data. It is important to carefully review your code and consider any external influences to determine the cause of the random changes.

2. How can I prevent my Fortran variable from changing randomly?

To prevent random changes to your Fortran variable, it is important to thoroughly test and debug your code to ensure there are no logical errors. You may also want to consider using the "save" statement to prevent the variable from being initialized to a random value each time the program is run. Additionally, you can use the "volatile" attribute to specify that a variable should not be optimized by the compiler, which can sometimes lead to unexpected changes.

3. Can compiler optimizations cause Fortran variable values to change randomly?

Yes, compiler optimizations can sometimes affect the behavior of Fortran variables. In some cases, the compiler may optimize certain parts of the code in a way that causes unexpected changes to the variable. To prevent this, you can use the "volatile" attribute to specify that a variable should not be optimized by the compiler.

4. Is it possible for Fortran variables to change randomly due to memory issues?

Yes, if there are memory issues in your code, it is possible for Fortran variables to change randomly. This can occur if the variable is not properly allocated or if there are issues with pointers or array indices. It is important to carefully manage memory in your code to avoid any unexpected changes to your variables.

5. How can I troubleshoot and fix random changes to my Fortran variable?

To troubleshoot and fix random changes to your Fortran variable, it is important to carefully review your code and look for any logical errors or external influences that may be causing the changes. You can also use debugging tools and techniques, such as printing the variable's value at different points in the code, to track down the source of the random changes. Once the issue is identified, you can make the necessary fixes to prevent the variable from changing randomly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
626
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
905
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top